none
c# dictionary issue when saving to a file. RRS feed

  • Question

  • ok i am creating a program that loads in a file splits to key value pairs and saves to a diffent format file and for the most part the program works perfectly but the issue im having is when i do 

    dict.Add("key1,"value"); manally it adds to list but on save it will not save out the ones i custom add this is the code i have 

    public void animadds()
            {
            elfweapon.Add(elfweapon.Properties, "idleAnim", Taiga.idle);
            }

    and this is the code that reads in the file.

    the load funtion

    and when it saves out it just dont include the line i added but includes the all the stuff the file has and i not sure how to get it to include my adds into it.

    this is a picture of what i mean

    as you can see form there it clearly added right at the bottom but here in the save out its clearly not there.

    {
            {
    
                    "weaponClass" "rifle"
    		"weaponType" "bullet"
    		"whizbySound" ""
    		"worldClipModel" "t6_attach_mag_an94_world"
    		"worldFlashEffect" "fx\weapon\fx_muz_sm_pistol_3p.efx"
    		"worldFlashOffsetX" "0"
    		"worldFlashOffsetY" "0"
    		"worldFlashOffsetZ" "0"
    		"worldLastShotEjectEffect" ""
    		"worldModel" ""
    		"worldModel1" ""
    		"worldModel10" ""
    		"worldModel11" ""
    		"worldModel12" ""
    		"worldModel13" ""
    		"worldModel14" ""
    		"worldModel15" ""
    		"worldModel2" ""
    		"worldModel3" ""
    		"worldModel4" ""
    		"worldModel5" ""
    		"worldModel6" ""
    		"worldModel7" ""
    		"worldModel8" ""
    		"worldModel9" ""
    		"worldShellEjectEffect" "fx\weapon\fx_shellejects_pistol.efx"
    		"worldShellEjectOffsetForward" "0"
    		"worldShellEjectOffsetRight" "0"
    		"worldShellEjectOffsetUp" "0"
    		"worldShellEjectRotationPitch" "0"
    		"worldShellEjectRotationRoll" "0"
    		"worldShellEjectRotationYaw" "0"
    		"yawConvergenceTime" "0"
    	}
    }

    any help into getting the dictunary.add to work would be much appeicated because i at a total loss to why its acting like this 

    thanks in advance elfenliedtopfan5


    Tuesday, July 23, 2019 12:29 AM

All replies

  • Greetings elfenliedtopfan2.

    I am having a lot of trouble following your code and understanding what the problem is, but I think it might be that when you save to the file, you are looping up to PropertyOrder.Count when you should be looping up to Properties.Count. Because when you add to Properties, PropertyOrder does not change. I think.

    • Proposed as answer by Tim Roberts Wednesday, July 24, 2019 10:55 PM
    Tuesday, July 23, 2019 1:24 AM
  • Hi 

    Thank you for posting here.

    I have seen the link you provided, the code are too long. could you provide a simple code with me? Even I could not find animadds' method in that link.

    I hope that you could provide the above information so that we could reproduce your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 23, 2019 5:50 AM
    Moderator
  • Hi 

    Thank you for posting here.

    I have seen the link you provided, the code are too long. could you provide a simple code with me? Even I could not find animadds' method in that link.

    I hope that you could provide the above information so that we could reproduce your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    that code provided is all a dll lib and the anim adds use the public void add()

    basicly what program does is load in a file split it and populate to key,value only thing i cant do is get my custom ones to add 

    but this is the function i use to call 

            public void weaponfileload()
                {
    
                elfenbo2 = new BO2_RES();
                elfenbo2.loadweaponfile();
                elfenbo2.testingname();
                elfweapon = Weapon.Load(Taiga.weaponfilepath);
                elfenbo2.defaultadds();
                animadds();
                elfweapon.SaveAssetToGDT(elfgdt);
                Taiga.elfgdt.Save(Application.StartupPath + @"\" + Taiga.filename + ".gdt");
                }

    weapon.load()

    		public static Weapon Load(string filePath)
    		{
    			bool flag = !File.Exists(filePath);
    			Weapon result;
    			if (flag)
    			{
    				result = null;
    			}
    			else
    			{
    				string extension = Path.GetExtension(filePath);
    				bool flag2 = !string.IsNullOrEmpty(extension);
    				if (flag2)
    				{
    					result = null;
    				}
    				else
    				{
    					string fileName = Path.GetFileName(filePath);
    					Weapon weapon = new Weapon(fileName);
    					string[] value = File.ReadAllLines(filePath);
    					string[] array = string.Join(" ", value).Split(new char[]
    					{
    						'\\'
    					});
    					bool flag3 = !array[0].Equals("WEAPONFILE", StringComparison.OrdinalIgnoreCase);
    					if (flag3)
    					{
    						result = null;
    					}
    					else
    					{
    						for (int i = 1; i < array.Length; i += 2)
    						{
    							string key = array[i];
    							string value2 = (i + 1 < array.Length) ? array[i + 1] : string.Empty;
    							Weapon.LoadKeyValue(weapon, key, value2);
    						}
    						result = weapon;
    					}
    				}
    			}
    			return result;
    		}

    elfweapon.SaveAssetToGDT(elfgdt);

    the code behind this is 

    		public Asset SaveAssetToGDT(GameDataTable gdt)
    		{
    			Asset asset = this.ToAsset();
    			gdt.AddAsset(asset);
    			return asset;
    		}


    Taiga.elfgdt.Save(Application.StartupPath + @"\" + Taiga.filename + ".gdt");
    		public void Save(string filePath)
    		{
    			using (StreamWriter streamWriter = new StreamWriter(filePath))
    			{
    				streamWriter.WriteLine("{");
    				foreach (string key in this.assetMap.Keys)
    				{
    					List<Asset> list = this.assetMap[key];
    					foreach (Asset asset in list)
    					{
    						streamWriter.WriteLine("\t\"{0}\" (\"{1}.gdf\")", asset.Name, asset.Type);
    						streamWriter.WriteLine("\t{");
    						foreach (KeyValuePair<string, string> keyValuePair in Asset.Properties)
    						{
    							streamWriter.WriteLine("\t\t\"{0}\" \"{1}\"", keyValuePair.Key, keyValuePair.Value);
    						}
    						streamWriter.WriteLine("\t}");
    					}
    				}
    				streamWriter.WriteLine("}");
    			}
    		}
    
    		// Token: 0x04000004 RID: 4
    		private Dictionary<string, List<Asset>> assetMap = new Dictionary<string, List<Asset>>();
    	}

    thats the process it goes though the 

    defaultadds()

    and anims()

    are functions i made to edit existing keys with new values before save ( that works file ) 

    but the .add function wont work it adds to list in debug but never gets saved to the file

            public void Add(Dictionary<string, string> Properties, string key, string value) // this will force add the anims elfenliedtopfan5.
            {
                if (Properties.ContainsKey(key))
                {
                    Properties[key] = value;
                }
                else
                {
                    Properties.Add(key, value);
                }
            }

    what i added to try and get the values to add in and save out ( does not work though ) 

    how you acess the dictunary.

                elfweapon.Add(elfweapon.Properties, "idleAnim", Taiga.idle);           

    elfweapon.Properties.Add("idleAnim", Taiga.idle);


                both ways wont work

    thanks in advance elfenliedtopfan5

    Tuesday, July 23, 2019 2:26 PM
  • Hi 

    Thanks for the feedback.

    I still could not reproduce your problem, but I make a simple code example for you.

     static void Main(string[] args)
            {
    
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("test1", "hi");
                string key = "test1";
                string value = "test2";
                Add(dic, key, value);
                string path = "D:\\test.txt";
                Save(path, dic);
    
                Console.ReadKey();
    
            }
            public static void Add(Dictionary<string, string> Properties, string key, string value) // this will force add the anims elfenliedtopfan5.
            {
                if (Properties.ContainsKey(key))
                {
                    Properties[key] = value;
                }
                else
                {
                    Properties.Add(key, value);
                }
            }
            public static void Save(string filePath,Dictionary<string,string> dic)
            {
                using (StreamWriter streamWriter = new StreamWriter(filePath))
                {
                    streamWriter.WriteLine("{");
                    foreach (var item in dic)
                    {
                        streamWriter.WriteLine("\t\t\"{0}\" \"{1}\"", item.Key, item.Value);
                    }
                    streamWriter.WriteLine("}");
                }
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 24, 2019 7:29 AM
    Moderator
  • Have you looked through the code in your decompiled ApexLib class?  The Save method does not save everything in the Properties dictionary.  It iterates through the PropertyOrder array, and saves the properties that are named there.  You bypassed the normal access methods and modified Properties directly.  You should be using elfWeapon.LoadKeyValue.  That updates the dictionary and the array.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 24, 2019 10:54 PM