Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Linq;
  6.  
  7. public class ItemList : MonoBehaviour
  8. {
  9.  
  10.     public static class fileName
  11.     {
  12.         public static string i = "Items";
  13.         public static string iv = "ItemsVars";
  14.     }
  15.  
  16.     public static class path
  17.     {
  18.         public static string ip = "Assets/Resources/Text/" + fileName.i + ".txt";
  19.         public static string ivp = "Assets/Resources/Text/" + fileName.iv + ".txt";
  20.     }
  21.  
  22.     public static class Rpath
  23.     {
  24.         public static string ir = "Text/" + fileName.i;
  25.         public static string ivr = "Text/" + fileName.iv;
  26.     }
  27.  
  28.     public static string ItemsPatch = "Items/";
  29.  
  30.     public class itemD
  31.     {
  32.         public string name;
  33.         public object value;
  34.         public object[] array;
  35.         public bool String;
  36.         public bool Int;
  37.         public bool Float;
  38.         public bool Double;
  39.         public bool Bool;
  40.         public bool stringArray;
  41.         public bool intArray;
  42.         public bool floatArray;
  43.         public bool doubleArray;
  44.         public bool boolArray;
  45.  
  46.         public bool find;
  47.     }
  48.  
  49.     public class dVar
  50.     {
  51.         public string name;
  52.         public int i;
  53.     }
  54.  
  55.     private static string[] v = {
  56.         "string", "int", "float", "double", "bool",
  57.         "string array", "int array", "float array", "double array", "bool array"
  58.     };
  59.  
  60.     public static List<dVar> LoadVars()
  61.     {
  62.         List<dVar> dList1 = new List<dVar>();
  63.  
  64.         TextAsset t = Resources.Load<TextAsset>(Rpath.ivr);
  65.  
  66.         if (t == null)
  67.             return dList1;
  68.  
  69.         string[] lines = t.text.Split('\n');
  70.         for (int i = 0; i < lines.Length - 1; i++)
  71.         {
  72.             string[] args = lines[i].Split('*');
  73.             dVar n = new dVar();
  74.             n.name = args[0];
  75.             n.i = int.Parse(args[1]);
  76.             dList1.Add(n);
  77.         }
  78.         return dList1;
  79.     }
  80.  
  81.     public static List<List<itemD>> Load()
  82.     {
  83.         List<List<itemD>> dItemList = new List<List<itemD>>();
  84.  
  85.         List<dVar> dList = ItemList.LoadVars();
  86.         if (dList == null)
  87.         {
  88.             Debug.LogError("Cant load items DB!");
  89.             return dItemList;
  90.         }
  91.  
  92.         TextAsset ta = Resources.Load<TextAsset>(Rpath.ir);
  93.         if (ta == null)
  94.         {
  95.             Debug.LogError("Cant find file '" + Rpath.ir + "'!");
  96.             return dItemList;
  97.         }
  98.  
  99.         string[] lines = ta.text.Split('\n');
  100.         for (int i = 0; i < lines.Length - 1; i++)
  101.         {
  102.             List<itemD> n = new List<itemD>();
  103.             string[] args = lines[i].Split(';');
  104.  
  105.             for (int x = 0; x < dList.Count; x++)
  106.             {
  107.                 n.Add(new itemD());
  108.                 n[n.Count - 1].name = dList[x].name;
  109.             }
  110.  
  111.             for (int ii = 0; ii < args.Length - 1; ii++)
  112.             {
  113.                 //itemD idd = new itemD();
  114.                 string[] args2 = args[ii].Split('=');
  115.                 string[] nargs = args2[0].Split('*');
  116.  
  117.                 string name = nargs[0];
  118.                 string typ = nargs[1];
  119.                 string value = args2[1];
  120.  
  121.                 for (int x = 0; x < n.Count; x++)
  122.                 {
  123.                     if (v[dList[x].i] == "string")
  124.                     {
  125.                         n[x].String = true;
  126.                         if (name == n[x].name)
  127.                             n[x].value = value;
  128.                     }
  129.                     else if (v[dList[x].i] == "int")
  130.                     {
  131.                         n[x].Int = true;
  132.                         if (name == n[x].name)
  133.                             n[x].value = int.Parse(value);
  134.                     }
  135.                     else if (v[dList[x].i] == "float")
  136.                     {
  137.                         n[x].Float = true;
  138.                         if (name == n[x].name)
  139.                             n[x].value = float.Parse(value);
  140.                     }
  141.                     else if (v[dList[x].i] == "double")
  142.                     {
  143.                         n[x].Double = true;
  144.                         if (name == n[x].name)
  145.                             n[x].value = double.Parse(value);
  146.                     }
  147.                     else if (v[dList[x].i] == "bool")
  148.                     {
  149.                         n[x].Bool = true;
  150.                         if (name == n[x].name)
  151.                             n[x].value = bool.Parse(value);
  152.                     }
  153.                     else if (v[dList[x].i] == "string array")
  154.                     {
  155.                         n[x].stringArray = true;
  156.                         if (name == n[x].name)
  157.                         {
  158.                             string[] s = value.Split(',');
  159.                             int iii = 0;
  160.                             foreach (string a in s)
  161.                             {
  162.                                 n[x].array[iii] = a;
  163.                             }
  164.  
  165.                         }
  166.                     }
  167.                     else if (v[dList[x].i] == "int array")
  168.                     {
  169.                         n[x].intArray = true;
  170.                         if (name == n[x].name)
  171.                         {
  172.                             string[] s = value.Split(',');
  173.                             int iii = 0;
  174.                             foreach (string a in s)
  175.                             {
  176.                                 n[x].array[iii] = int.Parse(a);
  177.                             }
  178.                         }
  179.                     }
  180.                     else if (v[dList[x].i] == "float array")
  181.                     {
  182.                         n[x].floatArray = true;
  183.                         if (name == n[x].name)
  184.                         {
  185.                             string[] s = value.Split(',');
  186.                             int iii = 0;
  187.                             foreach (string a in s)
  188.                             {
  189.                                 n[x].array[iii] = float.Parse(a);
  190.                             }
  191.                         }
  192.                     }
  193.                     else if (v[dList[x].i] == "double array")
  194.                     {
  195.                         n[x].doubleArray = true;
  196.                         if (name == n[x].name)
  197.                         {
  198.                             string[] s = value.Split(',');
  199.                             int iii = 0;
  200.                             foreach (string a in s)
  201.                             {
  202.                                 n[x].array[iii] = double.Parse(a);
  203.                             }
  204.                         }
  205.                     }
  206.                     else if (v[dList[x].i] == "bool array")
  207.                     {
  208.                         n[x].boolArray = true;
  209.                         if (name == n[x].name)
  210.                         {
  211.                             string[] s = value.Split(',');
  212.                             int iii = 0;
  213.                             foreach (string a in s)
  214.                             {
  215.                                 n[x].array[iii] = bool.Parse(a);
  216.                             }
  217.                         }
  218.                     }
  219.                 }
  220.             }
  221.             dItemList.Add(n);
  222.         }
  223.         return dItemList;
  224.     }
  225.  
  226.     private static Dictionary<string, List<itemD>> dItemList;
  227.     private List<string> saling = new List<string>();
  228.  
  229.     private void Awake()
  230.     {
  231.         dItemList = LoadDic();
  232.         saling = new List<string>();
  233.  
  234.         List<itemD>[] dil = dItemList.Values.ToArray();
  235.         string[] dils = dItemList.Keys.ToArray();
  236.  
  237.         for (int i = 0; i < dItemList.Count; i++)
  238.         {
  239.             List<itemD> liD = dil[i];
  240.             foreach (itemD dliD in liD)
  241.             {
  242.                 if (dliD.name == "sale")
  243.                 {
  244.                     if ((bool)dliD.value == true)
  245.                     {
  246.                         saling.Add(dils[i]);
  247.                         break;
  248.                     }
  249.                 }
  250.             }
  251.         }
  252.     }
  253.  
  254.     private static Dictionary<string, List<itemD>> LoadDic()
  255.     {
  256.         Dictionary<string, List<itemD>> dItemListD = new Dictionary<string, List<itemD>>();
  257.  
  258.         string code = "";
  259.  
  260.         List<dVar> dList = ItemList.LoadVars();
  261.         if (dList == null)
  262.         {
  263.             Debug.LogError("Cant load items DB!");
  264.             return dItemListD;
  265.         }
  266.  
  267.         TextAsset ta = Resources.Load<TextAsset>(Rpath.ir);
  268.         if (ta == null)
  269.         {
  270.             Debug.LogError("Cant find file '" + Rpath.ir + "'!");
  271.             return dItemListD;
  272.         }
  273.  
  274.         string[] lines = ta.text.Split('\n');
  275.         for (int i = 0; i < lines.Length - 1; i++)
  276.         {
  277.             List<itemD> n = new List<itemD>();
  278.             string[] args = lines[i].Split(';');
  279.  
  280.             for (int x = 0; x < dList.Count; x++)
  281.             {
  282.                 n.Add(new itemD());
  283.                 n[n.Count - 1].name = dList[x].name;
  284.             }
  285.  
  286.             for (int ii = 0; ii < args.Length - 1; ii++)
  287.             {
  288.                 //itemD idd = new itemD();
  289.                 string[] args2 = args[ii].Split('=');
  290.                 string[] nargs = args2[0].Split('*');
  291.  
  292.                 string name = nargs[0];
  293.                 string typ = nargs[1];
  294.                 string value = args2[1];
  295.  
  296.                 if (name == "code")
  297.                 {
  298.                     code = value;
  299.                 }
  300.  
  301.                 for (int x = 0; x < n.Count; x++)
  302.                 {
  303.                     if (v[dList[x].i] == "string")
  304.                     {
  305.                         n[x].String = true;
  306.                         if (name == n[x].name)
  307.                             n[x].value = value;
  308.                     }
  309.                     else if (v[dList[x].i] == "int")
  310.                     {
  311.                         n[x].Int = true;
  312.                         if (name == n[x].name)
  313.                             n[x].value = int.Parse(value);
  314.                     }
  315.                     else if (v[dList[x].i] == "float")
  316.                     {
  317.                         n[x].Float = true;
  318.                         if (name == n[x].name)
  319.                             n[x].value = float.Parse(value);
  320.                     }
  321.                     else if (v[dList[x].i] == "double")
  322.                     {
  323.                         n[x].Double = true;
  324.                         if (name == n[x].name)
  325.                             n[x].value = double.Parse(value);
  326.                     }
  327.                     else if (v[dList[x].i] == "bool")
  328.                     {
  329.                         n[x].Bool = true;
  330.                         if (name == n[x].name)
  331.                             n[x].value = bool.Parse(value);
  332.                     }
  333.                     else if (v[dList[x].i] == "string array")
  334.                     {
  335.                         n[x].stringArray = true;
  336.                         if (name == n[x].name)
  337.                         {
  338.                             string[] s = value.Split(',');
  339.                             int iii = 0;
  340.                             foreach (string a in s)
  341.                             {
  342.                                 n[x].array[iii] = a;
  343.                             }
  344.  
  345.                         }
  346.                     }
  347.                     else if (v[dList[x].i] == "int array")
  348.                     {
  349.                         n[x].intArray = true;
  350.                         if (name == n[x].name)
  351.                         {
  352.                             string[] s = value.Split(',');
  353.                             int iii = 0;
  354.                             foreach (string a in s)
  355.                             {
  356.                                 n[x].array[iii] = int.Parse(a);
  357.                             }
  358.                         }
  359.                     }
  360.                     else if (v[dList[x].i] == "float array")
  361.                     {
  362.                         n[x].floatArray = true;
  363.                         if (name == n[x].name)
  364.                         {
  365.                             string[] s = value.Split(',');
  366.                             int iii = 0;
  367.                             foreach (string a in s)
  368.                             {
  369.                                 n[x].array[iii] = float.Parse(a);
  370.                             }
  371.                         }
  372.                     }
  373.                     else if (v[dList[x].i] == "double array")
  374.                     {
  375.                         n[x].doubleArray = true;
  376.                         if (name == n[x].name)
  377.                         {
  378.                             string[] s = value.Split(',');
  379.                             int iii = 0;
  380.                             foreach (string a in s)
  381.                             {
  382.                                 n[x].array[iii] = double.Parse(a);
  383.                             }
  384.                         }
  385.                     }
  386.                     else if (v[dList[x].i] == "bool array")
  387.                     {
  388.                         n[x].boolArray = true;
  389.                         if (name == n[x].name)
  390.                         {
  391.                             string[] s = value.Split(',');
  392.                             int iii = 0;
  393.                             foreach (string a in s)
  394.                             {
  395.                                 n[x].array[iii] = bool.Parse(a);
  396.                             }
  397.                         }
  398.                     }
  399.                 }
  400.             }
  401.             dItemListD.Add(code, n);
  402.         }
  403.  
  404.         return dItemListD;
  405.     }
  406.  
  407.     public Transform SpawnItem(string code, Vector3 pos, Quaternion rot)
  408.     {
  409.         code = code.ToLower();
  410.         Transform o = null;
  411.         if (dItemList.ContainsKey(code))
  412.         {
  413.             List<itemD> ob = dItemList[code];
  414.             foreach (itemD id in ob)
  415.             {
  416.                 if (o != null)
  417.                 {
  418.                     if (id.name == "name")
  419.                         o.name = (string)id.value;
  420.                 }
  421.                 else
  422.                 {
  423.                     if (id.name == "prefab")
  424.                         o = Instantiate(Resources.Load<Transform>(ItemsPatch + (string)id.value), pos, rot);
  425.                 }
  426.             }
  427.  
  428.             Item d = o.gameObject.AddComponent<Item>();
  429.             d.Send(ob);
  430.         }
  431.         else
  432.         {
  433.             Debug.LogError("Item '" + code + "' dont exists in DB!");
  434.         }
  435.         return o;
  436.     }
  437.  
  438.     public Transform SpawnRandomSalingItem(Vector3 pos, Quaternion rot)
  439.     {
  440.         string code = saling[Random.Range(0, saling.Count)];
  441.  
  442.         Transform o = null;
  443.         List<itemD> ob = dItemList[code];
  444.         foreach (itemD id in ob)
  445.         {
  446.             if (o != null)
  447.             {
  448.                 if (id.name == "name")
  449.                     o.name = (string)id.value;
  450.             }
  451.             else
  452.             {
  453.                 if (id.name == "prefab")
  454.                 {
  455.                     o = Instantiate(Resources.Load<Transform>(ItemsPatch + (string)id.value), pos, rot);
  456.                     break;
  457.                 }
  458.             }
  459.         }
  460.  
  461.         Item d = o.gameObject.AddComponent<Item>();
  462.         d.Send(ob);
  463.  
  464.         return o;
  465.     }
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement