Skip to content

Commit

Permalink
Proper handling of invalid config
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTulach committed Jan 13, 2022
1 parent 6a8730c commit 861bd6d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions SoundReplacer/SoundReplacer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("ca60fd0d-b67b-4fd6-8081-3d8349bdcc89")]
[assembly: AssemblyVersion("0.0.5")]
[assembly: AssemblyFileVersion("0.0.5")]
[assembly: AssemblyVersion("0.0.6")]
[assembly: AssemblyFileVersion("0.0.6")]
32 changes: 26 additions & 6 deletions SoundReplacer/SoundReplacer/SoundLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public static void GetSoundLists()
}
}

public static string GetFullPath(string name)
private static string GetFullPath(string name)
{
var path = Environment.CurrentDirectory + "\\UserData\\SoundReplacer\\" + name;
var fileInfo = new FileInfo(path);
return fileInfo.FullName;
}

public static UnityWebRequest GetRequest(string fullPath)
private static UnityWebRequest GetRequest(string fullPath)
{
var fileUrl = "file:///" + fullPath;
var fileInfo = new FileInfo(fullPath);
Expand All @@ -62,6 +62,23 @@ public static UnityWebRequest GetRequest(string fullPath)
}
}

private static void ReplaceMissing(string name)
{
const string text = "Default";
if (Plugin.CurrentConfig.GoodHitSound == name)
Plugin.CurrentConfig.GoodHitSound = text;
if (Plugin.CurrentConfig.BadHitSound == name)
Plugin.CurrentConfig.BadHitSound = text;
if (Plugin.CurrentConfig.ClickSound == name)
Plugin.CurrentConfig.ClickSound = text;
if (Plugin.CurrentConfig.FailSound == name)
Plugin.CurrentConfig.FailSound = text;
if (Plugin.CurrentConfig.SuccessSound == name)
Plugin.CurrentConfig.SuccessSound = text;
if (Plugin.CurrentConfig.MenuMusic == name)
Plugin.CurrentConfig.MenuMusic = text;
}

public static AudioClip LoadAudioClip(string name)
{
var fullPath = GetFullPath(name);
Expand All @@ -75,11 +92,14 @@ public static AudioClip LoadAudioClip(string name)
// basically instant success or error
while (!task.isDone) { }

if (request.isNetworkError)
Plugin.Log.Error("Failed to load audio: " + request.error);
else
loadedAudio = DownloadHandlerAudioClip.GetContent(request);
if (request.isNetworkError || request.isHttpError)
{
Plugin.Log.Error($"Failed to load file {name} with error {request.error}");
ReplaceMissing(name);
return GetEmptyClip();
}

loadedAudio = DownloadHandlerAudioClip.GetContent(request);
return loadedAudio;
}

Expand Down
2 changes: 1 addition & 1 deletion SoundReplacer/SoundReplacer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "SoundReplacer",
"name": "SoundReplacer",
"author": "Otiosum",
"version": "0.0.5",
"version": "0.0.6",
"description": "Easily replace in-game sounds",
"gameVersion": "1.19.0",
"dependsOn": {
Expand Down

0 comments on commit 861bd6d

Please sign in to comment.