Skip to content

Commit

Permalink
more code warning cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftofZen committed Apr 8, 2024
1 parent 4701a21 commit 6ae7af0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
25 changes: 13 additions & 12 deletions Gui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ static string constructAnnotationText(Annotation annotation)
void LoadAndPlaySound(byte[] data, string soundName)
{
var (header, pcmData) = SawyerStreamReader.LoadWavFile(data);
var uiSoundObj = new UiSoundObject { Data = pcmData, Header = header, SoundName = soundName };
var uiSoundObjectList = new UiSoundObjectList();
var uiSoundObj = new UiSoundObject(soundName, header, pcmData);
var uiSoundObjectList = new UiSoundObjectList(soundName);
uiSoundObjectList.Audio.Add(uiSoundObj);
CurrentUIObject = uiSoundObjectList;

Expand All @@ -608,16 +608,16 @@ void LoadAndPlaySound(byte[] data, string soundName)
}
}

void LoadSoundEffectFile(byte[] data)
void LoadSoundEffectFile(byte[] data, string soundName)
{
var sfxs = SawyerStreamReader.LoadSoundEffectsFromCSS(data);

var i = 0;
var uiSoundObjectList = new UiSoundObjectList();
var uiSoundObjectList = new UiSoundObjectList(soundName);

foreach (var (header, pcmData) in sfxs)
{
var uiSoundObj = new UiSoundObject { Data = pcmData, Header = SawyerStreamWriter.WaveFormatExToRiff(header, pcmData.Length), SoundName = Enum.GetValues<SoundId>().ToList()[i++].ToString() };
var uiSoundObj = new UiSoundObject(Enum.GetValues<SoundId>().ToList()[i++].ToString(), SawyerStreamWriter.WaveFormatExToRiff(header, pcmData.Length), pcmData);
uiSoundObjectList.Audio.Add(uiSoundObj);
}

Expand Down Expand Up @@ -654,6 +654,7 @@ public void ImportWave(string soundNameToUpdate)
if (currentUIObject is UiSoundObjectList uiSoundObjList)
{
var soundObj = uiSoundObjList.Audio.Single(s => s.SoundName == soundNameToUpdate);
//soundObj = new UiSoundObject(soundNameToUpdate, header, pcmData);
soundObj.Header = header;
soundObj.Data = pcmData;
RefreshObjectUI();
Expand All @@ -664,10 +665,10 @@ public void ImportWave(string soundNameToUpdate)
{
logger.Info($"Replacing music track {soundNameToUpdate} with {openFileDialog.FileName}");
}
else
{
logger.Warning($"Sound name {soundNameToUpdate} was not recognised - no action will be taken.");
}
//else // commenting out because this falsely triggers for cs1.dat for sound effect names
//{
// logger.Warning($"Sound name {soundNameToUpdate} was not recognised - no action will be taken.");
//}
}
}
}
Expand Down Expand Up @@ -954,7 +955,7 @@ void LoadNull(string dataKey)

void LoadG1(string filename)
{
CurrentUIObject = new UiG1 { G1 = model.G1 };
CurrentUIObject = new UiG1(model.G1);
LoadDataDump(filename, true);
}

Expand Down Expand Up @@ -990,7 +991,7 @@ void tv_AfterSelect(object sender, TreeViewEventArgs e)
{
logger.Debug($"Loading sound effects for {e.Node.Name}");
var sfx = model.SoundEffects[e.Node.Name];
LoadSoundEffectFile(sfx);
LoadSoundEffectFile(sfx, e.Node.Text);
}
else if (Path.GetExtension(e.Node.Name).Equals(".dat", StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -1247,7 +1248,7 @@ void RefreshObjectUI()

var hdr = soundObject.SoundObjectData.PcmHeader;
var text = uiLocoObj.LocoObject.StringTable.Table["Name"][LanguageId.English_UK] ?? "<null>";
var pn = CreateSoundUI(new UiSoundObject { Data = soundObject.PcmData, Header = SawyerStreamWriter.WaveFormatExToRiff(hdr, soundObject.PcmData.Length), SoundName = text });
var pn = CreateSoundUI(new UiSoundObject(text, SawyerStreamWriter.WaveFormatExToRiff(hdr, soundObject.PcmData.Length), soundObject.PcmData));
flpImageTable.Controls.Add(pn);
}

Expand Down
4 changes: 2 additions & 2 deletions Gui/MainFormModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void CreateIndex(string[] allFiles, IProgress<float>? progress)
return;
}

if (!ccObjectCache.TryAdd(file, new UiLocoObject { DatFileInfo = fileInfo, LocoObject = locoObject }))
if (!ccObjectCache.TryAdd(file, new UiLocoObject(fileInfo, locoObject)))
{
logger.Warning($"Didn't add file {file} to cache - already exists (how???)");
}
Expand Down Expand Up @@ -364,7 +364,7 @@ static void SerialiseHeaderIndexToFile(string filename, HeaderIndex headerIndex,
else
{
var obj = SawyerStreamReader.LoadFullObjectFromFile(filename, logger: logger);
var uiObj = new UiLocoObject { DatFileInfo = obj.DatFileInfo, LocoObject = obj.LocoObject };
var uiObj = new UiLocoObject(obj.DatFileInfo, obj.LocoObject);
_ = ObjectCache.TryAdd(filename, uiObj);
return uiObj;
}
Expand Down
32 changes: 19 additions & 13 deletions Gui/UiLocoObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,46 @@ public interface IUiObjectWithGraphics
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class UiLocoObject : IUiObject, IUiObjectWithGraphics
public record UiLocoObject(DatFileInfo DatFileInfo, ILocoObject? LocoObject) : IUiObject, IUiObjectWithGraphics
{
public DatFileInfo DatFileInfo { get; set; }
public ILocoObject? LocoObject { get; set; }
public List<G1Element32> G1Elements
{
get => LocoObject?.G1Elements;
set => LocoObject.G1Elements = value;
get => LocoObject?.G1Elements ?? Enumerable.Empty<G1Element32>().ToList();
set
{
if (LocoObject != null)
{
LocoObject.G1Elements = value;
}
}
}
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class UiSoundObject
public record UiSoundObject(string SoundName)
{
public string SoundName { get; set; }
public UiSoundObject(string soundName, RiffWavHeader header, byte[] data) : this(soundName)
{
Header = header;
Data = data;
}

public RiffWavHeader Header { get; set; }
public byte[] Data { get; set; }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class UiSoundObjectList : IUiObject
public record UiSoundObjectList(string FileName) : IUiObject
{
public string FileName { get; set; }
public List<UiSoundObject> Audio { get; set; } = [];
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class UiG1 : IUiObject, IUiObjectWithGraphics
public record UiG1(G1Dat G1) : IUiObject, IUiObjectWithGraphics
{
public G1Dat G1 { get; set; }

public List<G1Element32> G1Elements
{
get => G1?.G1Elements;
get => G1?.G1Elements ?? Enumerable.Empty<G1Element32>().ToList();
set
{
G1.G1Elements.Clear();
Expand Down
10 changes: 8 additions & 2 deletions Gui/VersionCheckBody.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
namespace OpenLoco.ObjectEditor.Gui
using System.Text.Json.Serialization;

namespace OpenLoco.ObjectEditor.Gui
{
public record VersionCheckBody(string TagName);
public class VersionCheckBody
{
[JsonPropertyName("tag_name")]
public string TagName { get; set; }
}
}

0 comments on commit 6ae7af0

Please sign in to comment.