Skip to content

Commit

Permalink
data loader: better init
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCorby committed Aug 16, 2021
1 parent f83eca6 commit 0079833
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
1 change: 0 additions & 1 deletion Distance.LevelMods/CustomObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class CustomObjectManager : SerialComponent {
public Dictionary<string, byte[]> datas = new();

public override void Visit(IVisitor visitor, ISerializable prefabComp, int version) {
log.Debug($"visiting as {visitor.GetType().Name}");
switch (visitor) {
case BinarySerializer serializer:
serializer.VisitDictionaryGeneric(nameof(datas), ref datas,
Expand Down
55 changes: 28 additions & 27 deletions Distance.LevelMods/DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ private static SerialComponent LoadFromDll(byte[] data) {

var entryType = GetEntryType(entryTypes, $"dll {dll}");
var entryPrefab = Utils.NewPrefab(entryType.Namespace);
entryPrefab.SetActive(false);
return (SerialComponent)entryPrefab.AddComponent(entryType);
var entryComp = (SerialComponent)entryPrefab.AddComponent(entryType);
entryComp.CallInit($"dll {dll}");
return entryComp;
}

/// treat data as bundle, load dlls, find entry script.
Expand Down Expand Up @@ -96,42 +97,42 @@ private static SerialComponent GetEntryComp(AssetBundle bundle, Type entryType)
Path.GetFileNameWithoutExtension(asset) == entryType.Namespace?.ToLower());
GameObject entryPrefab;
if (asset == null) {
log.Debug($"no entry prefab found in bundle {bundle.name}. making empty one");
log.Debug($"no entry prefab {entryType.Namespace} found in bundle {bundle.name}. making empty one");
entryPrefab = Utils.NewPrefab(entryType.Namespace);
} else {
entryPrefab = bundle.LoadAsset<GameObject>(asset);
}

var entryComp = (SerialComponent)entryPrefab.AddComponent(entryType);
if (!entryComp.CallInit($"bundle {bundle.name}", bundle))
entryComp.CallInit($"bundle {bundle.name}");

foreach (var comp in entryPrefab.GetComponents<Component>())
if (!Serializer.IsComponentSerializable(comp))
log.Warning($"gameobject {entryPrefab.name} " +
log.Warning($"entry prefab {entryPrefab.name} " +
$"has non-serializable component {comp.GetType().FullName}");

var entryComp = (SerialComponent)entryPrefab.AddComponent(entryType);
return entryComp;
}

bool Call(string name, params object[] args) {
var signature = $"{name}({args.Join(arg => arg.GetType().Name)})";
try {
entryType.InvokeMember(name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod,
null, entryComp, args);
return true;
} catch (MissingMethodException) {
log.Warning($"entry type {entryType.FullName} in bundle {bundle.name} " +
$"does not have {signature} method");
return false;
} catch (AmbiguousMatchException) {
log.Warning($"entry type {entryType.FullName} in bundle {bundle.name} " +
$"has multiple {signature} methods");
return false;
}
private static bool CallInit(this SerialComponent entryComp, string container, params object[] args) {
const string name = "Init";
var signature = $"{name}({args.Join(arg => arg.GetType().Name)})";
try {
entryComp.GetType().InvokeMember(name,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod,
null, entryComp, args);
return true;
} catch (MissingMethodException) {
log.Warning($"entry comp {entryComp.GetType().FullName} in {container} " +
$"does not have {signature} method");
return false;
} catch (AmbiguousMatchException) {
log.Warning($"entry type {entryComp.GetType().FullName} in {container} " +
$"has multiple {signature} methods");
return false;
}

if (!Call("Init", bundle))
Call("Init");

return entryComp;
}
}
}

0 comments on commit 0079833

Please sign in to comment.