Skip to content

Commit

Permalink
解决主工程中基类构造函数访问脚本中覆盖方法的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
asheigithub committed May 5, 2018
1 parent 98025af commit b786237
Show file tree
Hide file tree
Showing 26 changed files with 239 additions and 37 deletions.
10 changes: 8 additions & 2 deletions ASCTest/testScript/AS3Testproj/src/Main.as
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,16 @@ class t2 extends ptt2
public function t2()
{
var m = b;
trace(m,b);
//trace("getvvv", getvvv() );
//var m = b;
}

override protected function get b():int
{
return 99;
}

override public function getvvv():int
{
return 456;
Expand All @@ -349,8 +355,8 @@ var t22:pttest = new t2();


var t:ttt = new ttt();
//trace(t.b2);
trace(t.b2);

t.test();
//.test();


2 changes: 2 additions & 0 deletions ASRuntime/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4159,6 +4159,8 @@ public object InvokeFunction(rtFunction function, int argcount, object v1, objec

}

public static Player _calling_icrossextendadapter_ctor_player;
public static ASBinCode.rtData.rtObjectBase _making_icrossextendadapter_obj;

public void MakeICrossExtendAdapterEnvironment(ICrossExtendAdapter adapter, ASBinCode.rtti.Class as3class)
{
Expand Down
Binary file modified AutoGenCodeLib/bin/Debug/AutoGenCodeLib.dll
Binary file not shown.
Binary file modified AutoGenCodeLib/obj/Debug/AutoGenCodeLib.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ E:\ASTool\AutoGenCodeLib\obj\Debug\AutoGenCodeTestLib.csprojResolveAssemblyRefer
E:\ASTool\AutoGenCodeLib\obj\Debug\AutoGenCodeLib.dll
E:\ASTool\AutoGenCodeLib\obj\Debug\AutoGenCodeLib.pdb
F:\ASTool\AutoGenCodeLib\bin\Debug\AutoGenCodeLib.xml
E:\ASTool\AutoGenCodeLib\bin\Debug\AutoGenCodeLib.xml
Binary file modified BuildOutSDK/1.0.0/bin/ASCompiler.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/bin/ASRuntime.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/bin/ASTool.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/bin/CMXMLCCLI.exe
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/linkcodegencli/ASCompiler.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/linkcodegencli/ASRuntime.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/linkcodegencli/ASTool.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/linkcodegencli/LinkCodeGen.dll
Binary file not shown.
Binary file modified BuildOutSDK/1.0.0/linkcodegencli/LinkCodeGenCLI.exe
Binary file not shown.
Binary file modified CMXMLCCLI/bin/Release/ASCompiler.dll
Binary file not shown.
Binary file modified CMXMLCCLI/bin/Release/ASRuntime.dll
Binary file not shown.
Binary file modified CMXMLCCLI/bin/Release/ASTool.dll
Binary file not shown.
Binary file modified CMXMLCCLI/bin/Release/CMXMLCCLI.exe
Binary file not shown.
Binary file modified CMXMLCCLI/obj/Release/CMXMLCCLI.exe
Binary file not shown.
149 changes: 127 additions & 22 deletions LinkCodeGen/ClassCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,41 @@

namespace LinkCodeGen
{
class MethodAndFName
{
public MethodInfo method;
public string name;
}

class ClassCreator:CreatorBase
{
protected byte[] xmlDoc;

private bool isSkipOverride(MethodInfo method)
{
if (method.Name == "OnValidate"
||
method.Name == "Reset"
)
{
var type = method.DeclaringType;
while (type != null)
{
if (NativeCodeCreatorBase.GetTypeFullName(type) == "UnityEngine.MonoBehaviour")
{
return true;
}
type = type.BaseType;
}


}

return false;
}



private bool isOverrideOrInherits(MethodInfo method)
{
if (!method.DeclaringType.Equals(type)
Expand All @@ -22,7 +53,9 @@ private bool isOverrideOrInherits(MethodInfo method)
}
return false;
}
private static Dictionary<Type, List<MethodAndFName>> dictVirtualMethods = new Dictionary<Type, List<MethodAndFName>>();

public static Dictionary<Type, string> dictTypeAdapterInterfaceName = new Dictionary<Type, string>();

public ClassCreator(Type classtype, string as3apidocpath, string csharpnativecodepath,
Dictionary<TypeKey, CreatorBase> typeCreators,
Expand Down Expand Up @@ -232,6 +265,15 @@ string linkcodenamespace
continue;
}

if (isOverrideOrInherits(method))
{
if ((method.IsAbstract))
{
donotaddprotectedctor = true;
}
continue;
}

//if (!method.Equals(method.GetBaseDefinition())) //override的,跳过
//{
// if ((method.IsAbstract))
Expand Down Expand Up @@ -430,7 +472,9 @@ string linkcodenamespace
}
}

if ((method.IsVirtual && !method.IsStatic && !(method.IsSpecialName && !isgetter) && !method.IsFinal) || method.IsAbstract)
if ((method.IsVirtual && !method.IsStatic && !(method.IsSpecialName && !isgetter) && !method.IsFinal
&& !isSkipOverride(method)
) || method.IsAbstract)
{
bool hasref = false;
//***如果有ref或out的方法则不能重写***
Expand All @@ -441,6 +485,9 @@ string linkcodenamespace
hasref = true;
break;
}



}
if (!hasref)
{
Expand All @@ -457,7 +504,9 @@ string linkcodenamespace
donotaddprotectedctor = true;
}
}
else if (!method.IsVirtual && method.IsFamily && !(method.IsSpecialName && !isgetter) && !method.IsStatic)
else if (!method.IsVirtual && method.IsFamily && !(method.IsSpecialName && !isgetter) && !method.IsStatic
&& !isSkipOverride(method)
)
{
bool hasref = false;
//***如果有ref或out的方法则不能重写***
Expand Down Expand Up @@ -694,6 +743,7 @@ public override string Create()

StringBuilder adapterfunc = new StringBuilder();
StringBuilder adapteroverridefunc = new StringBuilder();
StringBuilder interfacedefinefunc = new StringBuilder();

//GenNativeFuncImport(nativefunc);
GenNativeFuncNameSpaceAndClass(nativefunc);
Expand Down Expand Up @@ -1543,9 +1593,9 @@ public override string Create()

as3api.Append("();");
}
if (!type.IsSealed && !type.IsValueType && (constructorlist.Count >0 || protectedonstructorList.Count>0)

if (type !=typeof(System.MarshalByRefObject) && !type.IsSealed && !type.IsValueType && (constructorlist.Count > 0 || protectedonstructorList.Count > 0)

&&

(
Expand All @@ -1554,11 +1604,11 @@ public override string Create()
type.IsAbstract &&
(
(
constructorlist.Count>0 && constructorlist[0].GetParameters().Length !=0
constructorlist.Count > 0 && constructorlist[0].GetParameters().Length != 0
)
||
(
protectedonstructorList.Count >0 && protectedonstructorList[0].GetParameters().Length !=0
protectedonstructorList.Count > 0 && protectedonstructorList[0].GetParameters().Length != 0
)
)
)
Expand All @@ -1580,8 +1630,33 @@ public override string Create()
string adaptername = GetNativeFunctionPart1(type) + "Adapter";
string extendclassname = MethodNativeCodeCreatorBase.GetTypeFullName(type);


string adapterinterfacename = "I" + adaptername;
string namespacepart = linkcodenamespace + (string.IsNullOrEmpty(linkcodenamespace) ? "" : ".") + GetNativeFunctionClassName(type);

dictTypeAdapterInterfaceName.Add(type, namespacepart +"."+ adapterinterfacename);

adapterfunc.Append("\t\t");
adapterfunc.AppendLine("public class "+adaptername+" :"+extendclassname+" ,ASRuntime.ICrossExtendAdapter");
adapterfunc.AppendLine("public interface "+adapterinterfacename);
adapterfunc.Append("\t\t");
adapterfunc.AppendLine("{");
adapterfunc.AppendLine("[interfacedefine]");
adapterfunc.AppendLine("\t\t}");

var bt = type.BaseType;
while (bt !=null)
{
if (dictTypeAdapterInterfaceName.ContainsKey(bt))
{
adapterinterfacename += ", " + dictTypeAdapterInterfaceName[bt];
}
bt = bt.BaseType;
}



adapterfunc.Append("\t\t");
adapterfunc.AppendLine("public class "+adaptername+" :"+extendclassname+" ,ASRuntime.ICrossExtendAdapter, " + adapterinterfacename );
adapterfunc.Append("\t\t");
adapterfunc.AppendLine("{");

Expand Down Expand Up @@ -3111,22 +3186,45 @@ public void SetAS3RuntimeEnvironment(Player player, Class typeclass, ASBinCode.r

if (adapterfunc.Length > 0)
{


dictVirtualMethods.Add(type, new List<MethodAndFName>());
var toaddvirtuallist = dictVirtualMethods[type];

int vmindex=0;

//****函数重载***
//***写入基类的虚方法覆盖和公开受保护方法****
var bt = type.BaseType;
while ( bt !=null )
{
if (dictVirtualMethods.ContainsKey(bt))
{
foreach (var item in dictVirtualMethods[bt])
{
VirtualMethodNativeCodeCreator mc = new VirtualMethodNativeCodeCreator(item.method, bt, vmindex++, item.name);
adapteroverridefunc.AppendLine(mc.GetCode());
}
}

bt = bt.BaseType;
}


//****虚方法覆盖***
foreach (var item in virtualmethodlist)
{
if (methodas3names.ContainsKey(item))
{
VirtualMethodNativeCodeCreator mc = new VirtualMethodNativeCodeCreator(item, type, vmindex++, methodas3names[item]);
adapteroverridefunc.AppendLine(mc.GetCode());

toaddvirtuallist.Add(new MethodAndFName() { method=item, name = methodas3names[item] });

if (!item.IsPublic)
{
interfacedefinefunc.AppendLine(mc.GetPublicProtectedInterfaceDefine());
}
}
//else if (methodas3names.ContainsKey(item.GetBaseDefinition()))
//{
// VirtualMethodNativeCodeCreator mc = new VirtualMethodNativeCodeCreator(item, type, vmindex++, methodas3names[item.GetBaseDefinition()]);
// adapteroverridefunc.AppendLine(mc.GetCode());
//}

}

//****公开受保护的方法***
Expand All @@ -3136,19 +3234,26 @@ public void SetAS3RuntimeEnvironment(Player player, Class typeclass, ASBinCode.r
{
VirtualMethodNativeCodeCreator mc = new VirtualMethodNativeCodeCreator(item, type, vmindex++, methodas3names[item]);
adapteroverridefunc.AppendLine(mc.GetPublicProtectedCode());

interfacedefinefunc.AppendLine(mc.GetPublicProtectedInterfaceDefine());

toaddvirtuallist.Add(new MethodAndFName() { method = item, name = methodas3names[item] });
}
//else if (methodas3names.ContainsKey(item.GetBaseDefinition()))
//{
// VirtualMethodNativeCodeCreator mc = new VirtualMethodNativeCodeCreator(item, type, vmindex++, methodas3names[item.GetBaseDefinition()]);
// adapteroverridefunc.AppendLine(mc.GetPublicProtectedCode());
//}

}


}

nativefunc.AppendLine(adapterfunc.Replace("[overrides]",adapteroverridefunc.ToString()).ToString());

nativefunc.AppendLine(
adapterfunc.Replace("[overrides]",adapteroverridefunc.ToString())
.Replace("[interfacedefine]",interfacedefinefunc.ToString())
.ToString()



);



for (int i = 0; i < nativefuncClasses.Count; i++)
Expand Down
28 changes: 23 additions & 5 deletions LinkCodeGen/CreatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,13 +998,22 @@ protected static string GetMethodName(string dotName, System.Reflection.MethodIn
{
if (((FieldInfo)item).IsFamily)
{
if (IsSkipMember(item))
continue;
if (IsSkipType(((FieldInfo)item).FieldType))
continue;

_tempmembers.Add(item);
}
}
else if (item is MethodBase)
else if (item is MethodInfo)
{
if (((MethodBase)item).IsFamily)
if (((MethodInfo)item).IsFamily)
{
if (IsSkipMember(item))
continue;
if (InterfaceCreator.isMethodSkip((MethodInfo)item))
continue;
_tempmembers.Add(item);
}
}
Expand Down Expand Up @@ -1077,20 +1086,29 @@ protected static string GetMethodName(string dotName, System.Reflection.MethodIn
inheritmember.AddRange(inherit);

//***继承的受保护的对象***
var pinherit = basetype.GetMember(dotName, BindingFlags.Instance | BindingFlags.NonPublic);
var pinherit = basetype.GetMember(dotName, BindingFlags.Instance | BindingFlags.NonPublic );
foreach (var item in pinherit)
{
if (item is FieldInfo)
{
if (((FieldInfo)item).IsFamily)
{
if (IsSkipMember(item))
continue;
if (IsSkipType(((FieldInfo)item).FieldType))
continue;

inheritmember.Add(item);
}
}
else if (item is MethodBase)
else if (item is MethodInfo)
{
if (((MethodBase)item).IsFamily)
if (((MethodInfo)item).IsFamily)
{
if (IsSkipMember(item))
continue;
if (InterfaceCreator.isMethodSkip((MethodInfo)item))
continue;
inheritmember.Add(item);
}
}
Expand Down
2 changes: 1 addition & 1 deletion LinkCodeGen/MethodNativeCodeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public string GetCode()
{
loadthis = Properties.Resources.LoadThis;
//改为adapter的桥接的保护方法
loadthis = loadthis.Replace("[thisObjtype]", CreatorBase.GetNativeFunctionPart1(methodAtType) + "Adapter");
loadthis = loadthis.Replace("[thisObjtype]", ClassCreator.dictTypeAdapterInterfaceName[methodAtType]);
}


Expand Down

0 comments on commit b786237

Please sign in to comment.