Code cleanup.

This commit is contained in:
2022-11-17 17:21:59 +08:00
parent 34b17f3111
commit 777b7d9c34

View File

@@ -126,12 +126,12 @@ namespace Cryville.Crtr.Browsing {
resources = converter.ConvertFrom(file); resources = converter.ConvertFrom(file);
} }
catch (Exception ex) { catch (Exception ex) {
Popup.Create(ex.Message); LogAndPopup(4, ex.Message);
return false; return false;
} }
foreach (var res in resources) { foreach (var res in resources) {
if (!res.Valid) { if (!res.Valid) {
Logger.Log("main", 3, "Resource", "Attempt to import invalid resource {0}", res); LogAndPopup(3, "Attempt to import invalid resource: {0}", res);
} }
else if (res is RawChartResource) { else if (res is RawChartResource) {
var tres = (RawChartResource)res; var tres = (RawChartResource)res;
@@ -144,20 +144,27 @@ namespace Cryville.Crtr.Browsing {
writer.Write(JsonConvert.SerializeObject(tres.Meta, Game.GlobalJsonSerializerSettings)); writer.Write(JsonConvert.SerializeObject(tres.Meta, Game.GlobalJsonSerializerSettings));
} }
} }
else if (res is CoverResource) { else if (res is FileResource) {
var tres = (CoverResource)res; var tres = (FileResource)res;
var dir = new DirectoryInfo(_rootPath + "/charts/" + res.Name); FileInfo dest;
if (!dir.Exists) dir.Create(); // TODO chart, ruleset, skin
var dest = new FileInfo(_rootPath + "/charts/" + res.Name + "/" + tres.Source.Name); if (res is CoverResource)
if (!dest.Exists) tres.Source.CopyTo(dest.FullName); dest = new FileInfo(_rootPath + "/charts/" + res.Name + "/" + tres.Source.Name);
else if (res is SongResource)
dest = new FileInfo(_rootPath + "/songs/" + res.Name + "/" + tres.Source.Extension);
else {
LogAndPopup(3, "Attempt to import unsupported file resource: {0}", res);
continue;
}
var dir = dest.Directory;
if (!dest.Exists) {
if (!dir.Exists) dir.Create();
tres.Source.CopyTo(dest.FullName);
}
else LogAndPopup(1, "Resource already exists: {0}", res);
} }
else if (res is SongResource) { else {
var tres = (SongResource)res; LogAndPopup(3, "Attempt to import unsupported resource: {0}", res);
var dir = new DirectoryInfo(_rootPath + "/songs/" + res.Name);
if (!dir.Exists) dir.Create();
var dest = new FileInfo(_rootPath + "/songs/" + res.Name + "/" + tres.Source.Extension);
if (!dest.Exists) tres.Source.CopyTo(dest.FullName);
} }
} }
return true; return true;
@@ -165,6 +172,12 @@ namespace Cryville.Crtr.Browsing {
return false; return false;
} }
void LogAndPopup(int level, string format, params object[] args) {
var msg = string.Format(format, args);
Logger.Log("main", level, "Resource", msg);
Popup.Create(msg);
}
public string[] GetSupportedFormats() { public string[] GetSupportedFormats() {
return converters.Keys.ToArray(); return converters.Keys.ToArray();
} }