Skip to content

Commit

Permalink
Customizable aliases via IAliasCfg: +NoPrefixR, and fully updated beh…
Browse files Browse the repository at this point in the history
…aviour with prefixes /part of #9

* +NoPrefixR - to avoid prefix for right operand if it's defined:
```
l.Aliases["gmn"] = new ProcAlias(
    "GetMagicNum",
    new AliasCfg() { NoPrefixR = true } // false by default
);
```
  • Loading branch information
3F committed Jan 22, 2017
1 parent c676f47 commit 89ce52d
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 81 deletions.
38 changes: 38 additions & 0 deletions Conari/Aliases/AliasCfg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Denis Kuzmin <entry.reg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Conari"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace net.r_eg.Conari.Aliases
{
public struct AliasCfg: IAliasCfg
{
/// <summary>
/// Avoids prefix for right operand if it's defined.
/// </summary>
public bool NoPrefixR
{
get;
set;
}
}
}
80 changes: 80 additions & 0 deletions Conari/Aliases/AliasDict.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Denis Kuzmin <entry.reg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Conari"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using net.r_eg.Conari.Core;
using net.r_eg.Conari.Log;

namespace net.r_eg.Conari.Aliases
{
internal sealed class AliasDict
{
private Provider provider;

/// <summary>
/// The aliases for exported-functions and variables.
/// </summary>
public Dictionary<string, ProcAlias> Aliases
{
get;
private set;
} = new Dictionary<string, ProcAlias>();

/// <summary>
/// Try to use alias.
/// </summary>
/// <param name="lpProcName"></param>
/// <returns></returns>
public string use(LpProcName lpProcName)
{
if(Aliases == null || lpProcName.origin == null
|| !Aliases.ContainsKey(lpProcName.origin))
{
return (string)lpProcName;
}

IAlias als = Aliases[lpProcName.origin];
string ret;

if(als.Cfg != null && als.Cfg.NoPrefixR) {
ret = als.Name;
}
else {
ret = provider.procName(als.Name);
}

LSender.Send(this, $"Use alias '{ret}' instead of '{(string)lpProcName}'", Message.Level.Info);
return ret;
}

public AliasDict(Provider p)
{
if(p == null) {
throw new ArgumentException("Provider cannot be null.");
}
provider = p;
}
}
}
5 changes: 5 additions & 0 deletions Conari/Aliases/IAliasCfg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ namespace net.r_eg.Conari.Aliases
{
public interface IAliasCfg
{
/// <summary>
/// Avoids prefix for right operand if it's defined.
/// </summary>
bool NoPrefixR { get; set; }

//TODO: PrefixL/R, the rules of mangling, ...
}
}
1 change: 0 additions & 1 deletion Conari/Aliases/ProcAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public ProcAlias(string name)
Name = name;
}

// TODO: cfg
public ProcAlias(string name, IAliasCfg cfg)
: this(name)
{
Expand Down
3 changes: 3 additions & 0 deletions Conari/Conari.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Aliases\AliasCfg.cs" />
<Compile Include="Aliases\IAliasCfg.cs" />
<Compile Include="Aliases\AliasDict.cs" />
<Compile Include="Core\ExVar.cs" />
<Compile Include="Aliases\ProcAlias.cs" />
<Compile Include="Aliases\IAlias.cs" />
<Compile Include="Core\IExVar.cs" />
<Compile Include="Core\IDynamic.cs" />
<Compile Include="Core\IProviderSvc.cs" />
<Compile Include="Core\LpProcName.cs" />
<Compile Include="Core\ProcAddressArgs.cs" />
<Compile Include="Core\DataArgs.cs" />
<Compile Include="Core\IMem.cs" />
Expand Down
42 changes: 26 additions & 16 deletions Conari/Core/ExVar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public DefaultType getVar(string lpProcName)
/// <returns>The value from exported variable.</returns>
public T get<T>(string variable)
{
return getVar<T>(provider.procName(variable));
return getField(typeof(T), provider.Svc.procName(variable, true)).value;
}

/// <summary>
Expand All @@ -103,13 +103,7 @@ public DefaultType get(string variable)
/// <returns></returns>
public Native.Core.Field getField(Type type, string name)
{
return getField(
name,
provider
.Svc
.native(name)
.t(type, name)
);
return getField(type, provider.Svc.procName(name, false));
}

/// <summary>
Expand All @@ -136,13 +130,7 @@ public Native.Core.Field getField<T>(string name)
/// <returns></returns>
public Native.Core.Field getField(int size, string name)
{
return getField(
name,
provider
.Svc
.native(name)
.t(size, name)
);
return getField(size, provider.Svc.procName(name, false));
}

/// <summary>
Expand Down Expand Up @@ -218,7 +206,11 @@ public ExVar(IProvider p)

protected dynamic getFieldDLR(Type type, string variable)
{
return getField(type, provider.procName(variable)).value;
return getField(
type,
provider.Svc.tryAlias(variable)
)
.value;
}

protected Native.Core.Field getField(string name, NativeData n)
Expand All @@ -230,6 +222,24 @@ protected Native.Core.Field getField(string name, NativeData n)
return ret;
}

protected Native.Core.Field getField(Type type, LpProcName lpProcName)
{
return provider
.Svc
.native(lpProcName)
.t(type)
.Raw.Type.FirstField;
}

protected Native.Core.Field getField(int size, LpProcName lpProcName)
{
return provider
.Svc
.native(lpProcName)
.t(size)
.Raw.Type.FirstField;
}

private IEnumerable<Type> getGenericArgTypes(InvokeMemberBinder binder)
{
// FIXME: avoid access to private members
Expand Down
2 changes: 1 addition & 1 deletion Conari/Core/IBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface IBinder
/// Binds the exported Function via MethodInfo, an specific name and CallingConvention.
/// </summary>
/// <param name="mi">Prepared signature.</param>
/// <param name="name">Valid function name.</param>
/// <param name="name">Valid function name. Full name is required.</param>
/// <param name="conv">How it should be called. It overrides only for current method.</param>
/// <returns>Complete information to create delegates or to invoke methods.</returns>
TDyn bind(MethodInfo mi, string name, CallingConvention conv);
Expand Down
19 changes: 17 additions & 2 deletions Conari/Core/IProviderSvc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,28 @@ public interface IProviderSvc
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns>The address if found.</returns>
IntPtr getProcAddr(string lpProcName);
IntPtr getProcAddr(LpProcName lpProcName);

/// <summary>
/// Prepare NativeData for active provider.
/// </summary>
/// <param name="lpProcName">The name of function or variable, or the function's ordinal value.</param>
/// <returns></returns>
NativeData native(string lpProcName);
NativeData native(LpProcName lpProcName);

/// <summary>
/// Extracts LpProcName.
/// </summary>
/// <param name="lpProcName">Original lpProcName.</param>
/// <param name="prefix">Uses prefix.</param>
/// <returns></returns>
LpProcName procName(string lpProcName, bool prefix);

/// <summary>
/// Try to get alias.
/// </summary>
/// <param name="name">Possible alias name.</param>
/// <returns></returns>
string tryAlias(string name);
}
}
37 changes: 37 additions & 0 deletions Conari/Core/LpProcName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2017 Denis Kuzmin <entry.reg@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Conari"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace net.r_eg.Conari.Core
{
public struct LpProcName
{
public string origin;
public string prefixed;

public static explicit operator string(LpProcName proc)
{
return proc.prefixed ?? proc.origin;
}
}
}
Loading

0 comments on commit 89ce52d

Please sign in to comment.