Click here to Skip to main content
15,991,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an UMA recipe String basically an XML file and I don't want users to edit the file.

What I have tried:

                characterRecipe = dcAvatar.GetCurrentRecipe();
                FileUtils.WriteAllText(charRecipeFilePath, characterRecipe);

                string recipeFileText = File.ReadAllText(charRecipeFilePath);
                using (Stream recipeFileStream = File.Open(charRecipeFilePath, FileMode.Create))
                {
                    binaryFormatter.Serialize(recipeFileStream, recipeFileText);
                }

I don't get an error, but the file is now empty
Posted
Updated 24-Feb-20 1:02am
Comments
Richard MacCutchan 23-Feb-20 9:37am    
Did you check that you actually received some data in recipeFileText? Also, serializing text to a binary file will not change anything; people will still be able to edit it.
Member 7995828 24-Feb-20 7:27am    
I've noticed that. I created a class for some string data and I got an editable text file. I'll try changing the extension to .sav
Thanks

public class UmaInfoData
{
public string UmaInfo_name { get; set; }
public string UmaInfo_class { get; set; }
public string UmaInfo_race { get; set; }
public string UmaInfo_gender { get; set; }
public string UmaInfo_hp { get; set; }
public string UmaInfo_crestName { get; set; }
public string UmaInfo_weapon { get; set; }
public string UmaInfo_shield { get; set; }

public UmaInfoData() { }

public UmaInfoData(string name, string cclass, string race, string gender, string hp, string crestName, string weapon, string shield)
{
UmaInfo_name = name;
UmaInfo_class = cclass;
UmaInfo_race = race;
UmaInfo_gender = gender;
UmaInfo_hp = hp;
UmaInfo_weapon = weapon;
UmaInfo_shield = shield;
}
}


UmaInfoData charInfoData = new UmaInfoData
(
charInfo_Name.text,
classInfo_Text.text,
raceInfo_Text.text,
genderInfo_Text.text,
hpInfo_Text.text, crest.sprite.name,
rHandWeapon.name,
lHandItemName
);

using (FileStream infoFileStream = File.Create(charInfoFilePath))
{
binaryFormatter.Serialize(infoFileStream, charInfoData);
}

file contents:

 ÿÿÿÿ  FAssembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null UmaInfoData <umainfo_name>k__BackingField<umainfo_class>k__BackingField<umainfo_race>k__BackingField<umainfo_gender>k__BackingField<umainfo_hp>k__BackingField"<umainfo_crestname>k__BackingField<umainfo_weapon>k__BackingField<umainfo_shield>k__BackingField  Luna Thief Human Female 30

Alimer Axe Devaster Shield
Richard MacCutchan 24-Feb-20 7:32am    
You can change the extension to anything you like, it will not change the content of the file. If you make a file available to your users then you cannot easily prevent them from editing or deleting it. What exactly are you trying to achieve here?
Member 7995828 24-Feb-20 8:34am    
Basically, I don't want the player to change the fields in the file directly.
For example the Number of Hit Points: 30

I tried that:
The filepath is: string charRecipeFilePath = Application.persistentDataPath + "/Character Recipe" + "/" + player.name + ".txt";
characterRecipe = dcAvatar.GetCurrentRecipe();
//FileUtils.WriteAllText(charRecipeFilePath, characterRecipe);

using (FileStream recipeFileStream = File.Create(charRecipeFilePath))
{
    binaryFormatter.Serialize(recipeFileStream, characterRecipe);
}


The file Luna(Female)_CIROSTRIX15.txt contains:

   ÿÿÿÿ          Ë{"packedRecipeType":"DynamicCharacterAvatar","name":"Luna(Female)_CIROSTRIX15","race":"G3 Human Female","dna":[{"dnaType":"DynamicUMADna","dnaTypeHash":1673940569,"packedDna":"{\"bDnaAsset\":{\"instanceID\":38920},\"bDnaAssetName\":\"G3HF DynamicUMADnaAsset\",\"bDnaSettings\":[{\"name\":\"BodyDetails\",\"value\":128},{\"name\":\"BodySize\",\"value\":128},{\"name\":\"BodyBulk\",\"value\":128},{\"name\":\"BodyThickness\",\"value\":128},{\"name\":\"BodyHeight\",\"value\":128},{\"name\":\"BodyAnna\",\"value\":128},{\"name\":\"BodyBriley\",\"value\":128},{\"name\":\"BodyCathy\",\"value\":128},{\"name\":\"BodyClarice\",\"value\":128},{\"name\":\"BodyFilia\",\"value\":128},{\"name\":\"BodyGia\",\"value\":128},{\"name\":\"BodyIrelia\",\"value\":128},{\"name\":\"BodyJazmine\",\"value\":128},{\"name\":\"BodyLaura\",\"value\":128},{\"name\":\"BodyLindsey\",\"value\":128},{\"name\":\"BodyMonique\",\"value\":128},{\"name\":\"BodySenna\",\"value\":128},{\"name\":\"BodySunny\",\"value\":128},{\"name\":\"BodyTisha\",\"value\":128},{\"name\":\"BodyUma\",\"value\":128},{\"name\":\"HeadAnna\",\"value\":128},{\"name\":\"HeadBriley\",\"value\":128},{\"name\":\"HeadCathy\",\"value\":128},{\"name\":\"HeadClarice\",\"value\":128},{\"name\":\"HeadFilia\",\"value\":128},{\"name\":\"HeadGia\",\"value\":128},{\"name\":\"HeadIrelia\",\"value\":128},{\"name\":\"HeadJazmine\",\"value\":128},{\"name\":\"HeadLaura\",\"value\":128},{\"name\":\"HeadLindsey\",\"value\":128},{\"name\":\"HeadMonique\",\"value\":128},{\"name\":\"HeadSenna\",\"value\":128},{\"name\":\"HeadSunny\",\"value\":128},{\"name\":\"HeadTisha\",\"value\":128},{\"name\":\"HeadUMA\",\"value\":128},{\"name\":\"BreastSmall\",\"value\":128},{\"name\":\"BreastLarge\",\"value\":128},{\"name\":\"NipplesLength\",\"value\":128},{\"name\":\"NipplesSize\",\"value\":128},{\"name\":\"AreolaSize\",\"value\":128},{\"name\":\"GluteSize\",\"value\":128},{\"name\":\"HipSize\",\"value\":128},{\"name\":\"HeadElfEars\",\"value\":128},{\"name\":\"HeadSize\",\"value\":128}]}"}],"characterColors":[{"name":"Skin","colors":[240,210,159,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{"name":"Hair","colors":[50,30,2,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{"name":"Eyes","colors":[10,11,165,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]}],"wardrobeSet":[{"slot":"Hair","recipe":"G3F ShortToulouse Hair"},{"slot":"WardrobeCollection","recipe":"G3F JG_FullOutfit"}],"raceAnimatorController":"G3F AnimationCntrlr"}

Most of the file contents can be edited.
 
Share this answer
 
Comments
F-ES Sitecore 24-Feb-20 7:11am    
As you've already been told, making the file binary doesn't give it any protection nor stop people editing it. If you want to protect the file then encrypt it when it is saved and decrypt it when you load it.
Member 7995828 24-Feb-20 8:27am    
Thanks I'll try that.
Richard MacCutchan 24-Feb-20 7:35am    
That is not XML, it looks more like JSON, but not correctly formatted. Where did you get it from?
Member 7995828 24-Feb-20 8:10am    
It's part of the UMA framework.
characterRecipe = dcAvatar.GetCurrentRecipe();

{"packedRecipeType":"DynamicCharacterAvatar","name":"Luna(Female)_CIROSTRIX15","race":"G3 Human Female","dna":[{"dnaType":"DynamicUMADna","dnaTypeHash":1673940569,"packedDna":"{\"bDnaAsset\":{\"instanceID\":38920},\"bDnaAssetName\":\"G3HF DynamicUMADnaAsset\",\"bDnaSettings\":[{\"name\":\"BodyDetails\",\"value\":128},{\"name\":\"BodySize\",\"value\":128},{\"name\":\"BodyBulk\",\"value\":128},{\"name\":\"BodyThickness\",\"value\":128},{\"name\":\"BodyHeight\",\"value\":128},{\"name\":\"BodyAnna\",\"value\":128},{\"name\":\"BodyBriley\",\"value\":128},{\"name\":\"BodyCathy\",\"value\":128},{\"name\":\"BodyClarice\",\"value\":128},{\"name\":\"BodyFilia\",\"value\":128},{\"name\":\"BodyGia\",\"value\":128},{\"name\":\"BodyIrelia\",\"value\":128},{\"name\":\"BodyJazmine\",\"value\":128},{\"name\":\"BodyLaura\",\"value\":128},{\"name\":\"BodyLindsey\",\"value\":128},{\"name\":\"BodyMonique\",\"value\":128},{\"name\":\"BodySenna\",\"value\":128},{\"name\":\"BodySunny\",\"value\":128},{\"name\":\"BodyTisha\",\"value\":128},{\"name\":\"BodyUma\",\"value\":128},{\"name\":\"HeadAnna\",\"value\":128},{\"name\":\"HeadBriley\",\"value\":128},{\"name\":\"HeadCathy\",\"value\":128},{\"name\":\"HeadClarice\",\"value\":128},{\"name\":\"HeadFilia\",\"value\":128},{\"name\":\"HeadGia\",\"value\":128},{\"name\":\"HeadIrelia\",\"value\":128},{\"name\":\"HeadJazmine\",\"value\":128},{\"name\":\"HeadLaura\",\"value\":128},{\"name\":\"HeadLindsey\",\"value\":128},{\"name\":\"HeadMonique\",\"value\":128},{\"name\":\"HeadSenna\",\"value\":128},{\"name\":\"HeadSunny\",\"value\":128},{\"name\":\"HeadTisha\",\"value\":128},{\"name\":\"HeadUMA\",\"value\":128},{\"name\":\"BreastSmall\",\"value\":128},{\"name\":\"BreastLarge\",\"value\":128},{\"name\":\"NipplesLength\",\"value\":128},{\"name\":\"NipplesSize\",\"value\":128},{\"name\":\"AreolaSize\",\"value\":128},{\"name\":\"GluteSize\",\"value\":128},{\"name\":\"HipSize\",\"value\":128},{\"name\":\"HeadElfEars\",\"value\":128},{\"name\":\"HeadSize\",\"value\":128}]}"}],"characterColors":[{"name":"Skin","colors":[240,210,159,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{"name":"Hair","colors":[50,30,2,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]},{"name":"Eyes","colors":[10,11,165,255,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,0,0]}],"wardrobeSet":[{"slot":"Hair","recipe":"G3F ShortToulouse Hair"},{"slot":"WardrobeCollection","recipe":"G3F JG_FullOutfit"}],"raceAnimatorController":"G3F AnimationCntrlr"}


Richard MacCutchan 24-Feb-20 8:43am    
I have no idea what the UMA framework is, or what you are really trying to do.
Use the debugger to look at exactly what is going on: the most likely reason is that GetCurrentRecipe is returning an empty string, but we can't check that for you - the debugger can show you exactly what it returns, and that's important. It's also possible that your FileUtils class doesn't work properly eiterh, and we have no access to that either.

But ... why are you doing all that?
All you need is this:
C#
characterRecipe = dcAvatar.GetCurrentRecipe();
using (Stream recipeFileStream = File.Open(charRecipeFilePath, FileMode.Create))
{
    binaryFormatter.Serialize(recipeFileStream, characterRecipe);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900