Quantum-Engineering/BlockDictionary.cs

152 lines
6.6 KiB
C#
Raw Normal View History

2020-08-27 02:08:41 -04:00
using UnityEngine;
2020-08-29 02:15:38 -04:00
using System;
using System.Linq;
2020-08-27 02:08:41 -04:00
using System.Collections.Generic;
using System.IO;
2020-08-27 02:08:41 -04:00
2020-09-16 21:40:25 -05:00
//! This class contains GameObject dictionaries for easily referencing different machines and other blocks.
public class BlockDictionary
2020-08-27 02:08:41 -04:00
{
private PlayerController playerController;
2020-08-27 05:06:16 -04:00
public Dictionary<string, GameObject> machineDictionary;
public Dictionary<string, GameObject> blockDictionary;
2020-08-27 02:08:41 -04:00
public BlockDictionary(PlayerController playerController)
2020-08-27 02:08:41 -04:00
{
this.playerController = playerController;
Init();
}
2020-08-27 05:06:16 -04:00
2020-09-16 21:40:25 -05:00
//! Gets description for a specfic machine.
2020-09-13 02:45:50 -04:00
public string GetMachineDescription(string machineName)
{
string modPath = Path.Combine(Application.persistentDataPath, "Mods");
Directory.CreateDirectory(modPath);
2020-09-13 02:45:50 -04:00
string[] modDirs = Directory.GetDirectories(modPath);
foreach (string path in modDirs)
{
string machinePath = path + "/Machines/";
DirectoryInfo d = new DirectoryInfo(machinePath);
foreach (FileInfo file in d.GetFiles("*.qe"))
{
string filePath = machinePath + file.Name;
string fileContents = File.ReadAllText(filePath);
string fileName = file.Name.Remove(file.Name.Length - 3);
if (fileName == machineName)
{
string modName = new DirectoryInfo(path).Name;
2020-09-13 02:45:50 -04:00
return fileContents.Split(']')[0].Substring(1);
}
}
}
Debug.Log("Failed to get description for [" + machineName + "]");
2020-09-13 02:45:50 -04:00
return null;
}
2020-09-16 21:40:25 -05:00
//! Gets recipes for a specfic machine.
public BasicMachineRecipe[] GetMachineRecipes(string machineName)
{
string modPath = Path.Combine(Application.persistentDataPath, "Mods");
Directory.CreateDirectory(modPath);
string[] modDirs = Directory.GetDirectories(modPath);
foreach (string path in modDirs)
{
string machinePath = path + "/Machines/";
DirectoryInfo d = new DirectoryInfo(machinePath);
foreach (FileInfo file in d.GetFiles("*.qe"))
{
string filePath = machinePath + file.Name;
string fileContents = File.ReadAllText(filePath);
2020-09-13 02:45:50 -04:00
string fileName = file.Name.Remove(file.Name.Length - 3);
if (fileName == machineName)
{
2020-09-13 02:45:50 -04:00
string recipeContents = fileContents.Split(']')[1];
string[] machineContents = recipeContents.Split('}');
BasicMachineRecipe[] machineRecipes = new BasicMachineRecipe[machineContents.Length - 1];
for (int i = 0; i < machineRecipes.Length; i++)
{
string input = machineContents[i].Split(':')[0];
string output = machineContents[i].Split(':')[1];
machineRecipes[i] = new BasicMachineRecipe(input, output);
}
string modName = new DirectoryInfo(path).Name;
Debug.Log(modName+" added recipes for [" + machineName + "]");
return machineRecipes;
}
}
}
return null;
}
2020-09-16 21:40:25 -05:00
//! Adds machines from mods to the game.
public void AddModMachines(Dictionary<string, GameObject> dictionary)
{
string modPath = Path.Combine(Application.persistentDataPath, "Mods");
Directory.CreateDirectory(modPath);
string[] modDirs = Directory.GetDirectories(modPath);
foreach (string path in modDirs)
{
string machinePath = path + "/Machines/";
DirectoryInfo d = new DirectoryInfo(machinePath);
foreach (FileInfo file in d.GetFiles("*.qe"))
{
string filePath = machinePath + file.Name;
string fileContents = File.ReadAllText(filePath);
2020-09-13 02:45:50 -04:00
string machineName = file.Name.Remove(file.Name.Length - 3);
if (!dictionary.ContainsKey(machineName))
{
dictionary.Add(machineName, playerController.modMachine);
List<string> objList = playerController.blockSelector.objectNames.ToList();
objList.Add(machineName);
playerController.blockSelector.objectNames = objList.ToArray();
string modName = new DirectoryInfo(path).Name;
Debug.Log(modName+" created a new machine: [" + machineName + "]");
2020-09-13 02:45:50 -04:00
}
}
}
}
2020-09-16 21:40:25 -05:00
//! Initializes variables.
private void Init()
{
2020-08-27 05:06:16 -04:00
blockDictionary = new Dictionary<string, GameObject>
{
{ "Brick", playerController.brick },
{ "Glass Block", playerController.glass },
{ "Iron Block", playerController.ironBlock },
{ "Iron Ramp", playerController.ironRamp },
{ "Steel Block", playerController.steel },
{ "Steel Ramp", playerController.steelRamp }
};
machineDictionary = new Dictionary<string, GameObject>
2020-08-27 02:08:41 -04:00
{
{ "Quantum Hatchway", playerController.airlock },
{ "Alloy Smelter", playerController.alloySmelter },
{ "Auger", playerController.auger },
{ "Auto Crafter", playerController.autoCrafter },
{ "Dark Matter Collector", playerController.darkMatterCollector },
{ "Dark Matter Conduit", playerController.darkMatterConduit },
{ "Electric Light", playerController.electricLight },
{ "Extruder", playerController.extruder },
{ "Gear Cutter", playerController.gearCutter },
{ "Generator", playerController.generator },
{ "Heat Exchanger", playerController.heatExchanger },
{ "Nuclear Reactor", playerController.nuclearReactor },
{ "Power Conduit", playerController.powerConduit },
{ "Press", playerController.press },
{ "Rail Cart", playerController.railCart },
{ "Rail Cart Hub", playerController.railCartHub },
{ "Reactor Turbine", playerController.reactorTurbine },
{ "Retriever", playerController.retriever },
{ "Smelter", playerController.smelter },
{ "Solar Panel", playerController.solarPanel },
{ "Storage Computer", playerController.storageComputer },
2020-08-27 05:06:16 -04:00
{ "Storage Container", playerController.storageContainer },
{ "Turret", playerController.turret },
2020-08-27 02:08:41 -04:00
{ "Universal Conduit", playerController.universalConduit },
{ "Universal Extractor", playerController.universalExtractor }
};
}
}