Skip to content

Commit

Permalink
Implementing (preliminary) support for ReStock.
Browse files Browse the repository at this point in the history
For issue #1
  • Loading branch information
Lisias committed Oct 1, 2021
1 parent 60a365d commit 5b06bf3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 35 deletions.
34 changes: 28 additions & 6 deletions Source/MeshEngine/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,26 @@ namespace DistantObject.MeshEngine
{
public static class Database
{
internal static readonly Dictionary<string, string> partModel = new Dictionary<string, string>();
internal static class PartModelDB
{
private static readonly Dictionary<string, List<string>> DB = new Dictionary<string, List<string>>();

internal static void Add(string partName, string modelName)
{
if (!DB.ContainsKey(partName)) DB.Add(partName, new List<string>());
DB[partName].Add(modelName);
}

internal static bool ContainsKey(string partName)
{
return DB.ContainsKey(partName);
}

internal static IEnumerable<string> Get(string partName)
{
return DB[partName];
}
}

internal static void Init()
{
Expand Down Expand Up @@ -45,14 +64,17 @@ internal static void Init()
string modelName = cfgNode.GetValue("mesh");
modelName = System.IO.Path.GetFileNameWithoutExtension(modelName);
Log.detail("Addint {0} {1}/{2}", partName, url, modelName);
partModel.Add(partName, url + "/" + modelName);
PartModelDB.Add(partName, url + "/" + modelName);
}
else if (cfgNode.HasNode("MODEL"))
{
ConfigNode cn = cfgNode.GetNode("MODEL");
string modelName = cn?.GetValue("model");
Log.detail("Addint {0} {1}", partName, modelName);
partModel.Add(partName, modelName);
ConfigNode[] cna = cfgNode.GetNodes("MODEL");
foreach (ConfigNode cn in cna)
{
string modelName = cn?.GetValue("model");
Log.detail("Addint {0} {1}", partName, modelName);
PartModelDB.Add(partName, modelName);
}
}
else
{
Expand Down
61 changes: 32 additions & 29 deletions Source/MeshEngine/Implementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,50 @@ void DistantObject.Contract.MeshEngine.Interface.Draw()
continue;
}

if (!Database.partModel.ContainsKey(partName))
if (!Database.PartModelDB.ContainsKey(partName))
{
partName = partName.Replace('.', '_');
if (!Database.partModel.ContainsKey(partName))
if (!Database.PartModelDB.ContainsKey(partName))
{
Log.detail("DistObj ERROR: Could not find config definition for {0}", partName);
Log.error("Could not find config definition for {0}", partName);
continue;
}
}

GameObject clone = GameDatabase.Instance.GetModel(Database.partModel[partName]);
if (clone == null)
{
Log.detail("DistObj ERROR: Could not load part model {0}", Database.partModel[partName]);
continue;
}
foreach(string modelName in Database.PartModelDB.Get(partName))
{
GameObject clone = GameDatabase.Instance.GetModel(modelName);
if (clone == null)
{
Log.error("Could not load part model {0}", Database.PartModelDB.Get(modelName));
continue;
}

GameObject cloneMesh = Mesh.Instantiate(clone) as GameObject;
clone.DestroyGameObject();
cloneMesh.transform.SetParent(this.vessel.transform);
cloneMesh.transform.localPosition = a.position;
cloneMesh.transform.localRotation = a.rotation;
GameObject cloneMesh = Mesh.Instantiate(clone) as GameObject;
clone.DestroyGameObject();
cloneMesh.transform.SetParent(this.vessel.transform);
cloneMesh.transform.localPosition = a.position;
cloneMesh.transform.localRotation = a.rotation;

VesselRanges.Situation situation = this.vessel.vesselRanges.GetSituationRanges(this.vessel.situation);
if (Vector3d.Distance(cloneMesh.transform.position, FlightGlobals.ship_position) < situation.load)
{
Log.error("Tried to draw part {0} within rendering distance of active vessel!", partName);
continue;
}
cloneMesh.SetActive(true);
VesselRanges.Situation situation = this.vessel.vesselRanges.GetSituationRanges(this.vessel.situation);
if (Vector3d.Distance(cloneMesh.transform.position, FlightGlobals.ship_position) < situation.load)
{
Log.error("Tried to draw part {0} within rendering distance of active vessel!", partName);
continue;
}
cloneMesh.SetActive(true);

foreach (Collider col in cloneMesh.GetComponentsInChildren<Collider>())
{
col.enabled = false;
}
foreach (Collider col in cloneMesh.GetComponentsInChildren<Collider>())
{
col.enabled = false;
}

foreach (ProtoPartModuleSnapshot module in a.modules)
cloneMesh = DistantObject.MeshEngine.Contract.Module.Render(cloneMesh, a, avPart, module);
foreach (ProtoPartModuleSnapshot module in a.modules)
cloneMesh = DistantObject.MeshEngine.Contract.Module.Render(cloneMesh, a, avPart, module);

this.referencePart.Add(cloneMesh, a);
this.meshList.Add(cloneMesh);
this.referencePart.Add(cloneMesh, a);
this.meshList.Add(cloneMesh);
}
}
}

Expand Down

0 comments on commit 5b06bf3

Please sign in to comment.