0

For those of you familiar with Minecraft, the 1.8 update stores the sounds as a file with an encrypted hash as the name (which you can really just change the extension to .ogg to play). There is an index stored as a JSON file in the assets folder which shows the proper sound name for each file with the encrypted hash name.

I'm trying to create a program that which the user types the name and it will find the sound(s) that contains that name. The index is stored in this fashion:

{ "objects":{"minecraft/sounds/mob/wither/idle2.ogg": {
  "hash": "6b2f86a35a3cd88320b55c029d77659915f83239",
  "size": 19332
},
"minecraft/lang/fil_PH.lang": {
  "hash": "e2c8f26c91005a795c08344d601b10c84936e89d",
  "size": 74035
},
"minecraft/sounds/note/snare.ogg": {
  "hash": "6967f0af60f480e81d32f1f8e5f88ccafec3a40c",
  "size": 3969
},
"minecraft/sounds/mob/villager/idle1.ogg": {
  "hash": "a772db3c8ac37dfeb3a761854fb96297257930ab",
  "size": 8605
},
"minecraft/sounds/mob/wither/hurt3.ogg": {
  "hash": "a4cf4ebe4c475cd6a4852d6b4228a4b64cf5cb00",
  "size": 16731
}

For example if the user types wither, it will grab the hashes for "minecraft/sounds/mob/wither/idle2.ogg" and "minecraft/sounds/mob/wither/hurt3.ogg"

My question is, how do I get the object names (the names, not the properties) to compare with the user's keyword string.

Sorry if I didn't use proper terminology for some words, I don't tinker with JSON files much. Correct my terminology as needed.

rakagunarto
  • 325
  • 3
  • 23

2 Answers2

0

EDIT

This answer solves it a lot more nicely (without dynamic):

https://stackoverflow.com/a/32129497/563532

Original answer:

This works:

var obj = JsonConvert.DeserializeObject<dynamic>(@"{ ""objects"":{""minecraft/sounds/mob/wither/idle2.ogg"": {
  ""hash"": ""6b2f86a35a3cd88320b55c029d77659915f83239"",
  ""size"": 19332
},
""minecraft/lang/fil_PH.lang"": {
  ""hash"": ""e2c8f26c91005a795c08344d601b10c84936e89d"",
  ""size"": 74035
},
""minecraft/sounds/note/snare.ogg"": {
  ""hash"": ""6967f0af60f480e81d32f1f8e5f88ccafec3a40c"",
  ""size"": 3969
},
""minecraft/sounds/mob/villager/idle1.ogg"": {
  ""hash"": ""a772db3c8ac37dfeb3a761854fb96297257930ab"",
  ""size"": 8605
},
""minecraft/sounds/mob/wither/hurt3.ogg"": {
  ""hash"": ""a4cf4ebe4c475cd6a4852d6b4228a4b64cf5cb00"",
  ""size"": 16731
}
}
}");

var t = obj.objects;
var names = new HashSet<String>();
foreach(JProperty fileThing in t)
{
    names.Add(fileThing.Name);
}
names.Dump();

Gives:

minecraft/sounds/mob/wither/idle2.ogg
minecraft/lang/fil_PH.lang
minecraft/sounds/note/snare.ogg
minecraft/sounds/mob/villager/idle1.ogg
minecraft/sounds/mob/wither/hurt3.ogg

You can also do this:

var t = obj.objects;
var names = new Dictionary<String, String>();
foreach(JProperty fileThing in t)
{
    names.Add(fileThing.Name, (string)t[fileThing.Name].hash);
}

Which gives you a dictionary linking the original name to the hash:

minecraft/sounds/mob/wither/idle2.ogg -> 6b2f86a35a3cd88320b55c029d77659915f83239 minecraft/lang/fil_PH.lang -> e2c8f26c91005a795c08344d601b10c84936e89d minecraft/sounds/note/snare.ogg -> 6967f0af60f480e81d32f1f8e5f88ccafec3a40c minecraft/sounds/mob/villager/idle1.ogg -> a772db3c8ac37dfeb3a761854fb96297257930ab minecraft/sounds/mob/wither/hurt3.ogg -> a4cf4ebe4c475cd6a4852d6b4228a4b64cf5cb00

Community
  • 1
  • 1
Rob
  • 26,989
  • 16
  • 82
  • 98
  • Thank you so much! But how can I manipulate fileThing.Name easily to only copy the filename? like idle2.ogg and snare.ogg only? so when I copy it over from the source folder it doesn't copy to the destination folder as the whole path name (if that makes any sense but you know what I mean right?). – rakagunarto Aug 19 '15 at 03:12
  • A quick & dirty solution would be to do this: `names.Add(fileThing.Name.Substring(fileThing.Name.LastIndexOf("/") + 1), (string)t[fileThing.Name].hash);` – Rob Aug 19 '15 at 03:16
  • Thx but I found how using System.IO.Path.GetFileName(string path) – rakagunarto Aug 19 '15 at 03:18
0

Assuming you have a jsonString as a string variable.

jsonString = "";

JArray array = JArray.Parse(json);

foreach (JObject content in array.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
        Console.WriteLine(prop.Name);
    }
}
Muks
  • 134
  • 9