Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeLab committed Aug 20, 2022
1 parent 03f4a63 commit 6013549
Show file tree
Hide file tree
Showing 24 changed files with 324 additions and 388 deletions.
2 changes: 1 addition & 1 deletion 99x8Edit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "99x8Edit", "99x8Edit\99x8Edit.csproj", "{FBBA5228-312B-4C5E-8AE0-BBF2F3ED77DF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "99x8Edit", "99x8Edit\99x8Edit.csproj", "{FBBA5228-312B-4C5E-8AE0-BBF2F3ED77DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
7 changes: 1 addition & 6 deletions 99x8Edit/About.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

Expand All @@ -19,7 +14,7 @@ public About()
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
Version ver = asm.GetName().Version;
// Currently it's alpha version
lblVer.Text = "ver" + ver.ToString() + " alpha";
lblVer.Text = "ver" + ver?.ToString() + " alpha";
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Expand Down
3 changes: 1 addition & 2 deletions 99x8Edit/Clipboard.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace _99x8Edit
{
internal class ClipboardWrapper
{
internal static readonly string _clipID = "99x8Edit";
private static readonly string _clipID = "99x8Edit";
// For copy and paste actions
internal static void SetData(ClipBase clip)
{
Expand Down
21 changes: 16 additions & 5 deletions 99x8Edit/Compression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace _99x8Edit
Expand Down Expand Up @@ -44,15 +43,19 @@ public override byte[] Encode(byte[] source)
int count = 0;
for (int i = 0; i < source.Length; ++i)
{
if ((int)(source[i]) != current_data)
if (source[i] != current_data)
{
if (count > 0) // New type of data started
{
outputData.Add((byte)count); // Record size and value of previous data
if (current_data == -1) // Not expected to happen. Fail safe
{
throw new Exception("Compression failed.");
}
outputData.Add((byte)count); // Record size and value of previous data
outputData.Add((byte)current_data);
}
count = 1;
current_data = (int)source[i];
current_data = source[i];
}
else
{
Expand All @@ -68,6 +71,10 @@ public override byte[] Encode(byte[] source)
}
}
}
if (current_data == -1) // Not expected to happen. Fail safe
{
throw new Exception("Compression failed.");
}
outputData.Add((byte)count); // Record size and value of previous data
outputData.Add((byte)current_data);
return outputData.ToArray();
Expand All @@ -82,6 +89,10 @@ public override byte[] Encode(byte[] source)
// for output
Dictionary<byte, int> pair_table = new Dictionary<byte, int>();
byte[] work_buffer = source.Clone() as byte[]; // Default output buffer = input data
if (work_buffer == null)
{
throw new Exception("Compression failed.");
}
int[] chr_used = new int[256];
int compressed_size = work_buffer.Length;
do
Expand Down Expand Up @@ -154,7 +165,7 @@ public override byte[] Encode(byte[] source)
foreach (KeyValuePair<byte, int> kvp in pair_table)
{
// Output dictionary
ret.Add((byte)(kvp.Key));
ret.Add(kvp.Key);
ret.Add((byte)(kvp.Value >> 8));
ret.Add((byte)(kvp.Value & 0xFF));
}
Expand Down
21 changes: 13 additions & 8 deletions 99x8Edit/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -10,9 +8,9 @@ namespace _99x8Edit
{
public class ConfigRoot
{
public IndivisualSettings Settings { get; set; } = new IndivisualSettings();
public IndividualSettings Settings { get; set; } = new IndividualSettings();
}
public class IndivisualSettings
public class IndividualSettings
{
public string ProjectFile { get; set; } = "";
public string ImportDirectory { get; set; } = "";
Expand All @@ -33,7 +31,7 @@ internal class Config
{
private static readonly string _filename = "appsettings.json";
private static ConfigRoot _config = new ConfigRoot();
internal static IndivisualSettings Setting
internal static IndividualSettings Setting
{
get => _config.Settings;
}
Expand All @@ -48,7 +46,10 @@ internal static void Load()
.Build()
.Get<ConfigRoot>();
}
catch {}
catch (Exception)
{
// Do nothing cause this may occur by invalid app setting file
}
}
internal static void Save()
{
Expand All @@ -59,8 +60,12 @@ internal static void Save()
};
jsonWriteOptions.Converters.Add(new JsonStringEnumConverter());
string json = JsonSerializer.Serialize(_config, jsonWriteOptions);
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _filename);
File.WriteAllText(path, json);
string appdir = AppDomain.CurrentDomain.BaseDirectory;
if (appdir != null)
{
string path = Path.Combine(appdir, _filename);
File.WriteAllText(path, json);
}
}
}
}
11 changes: 3 additions & 8 deletions 99x8Edit/CursorAnimation.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace _99x8Edit
{
internal partial class CursorAnimation : Form
{
// Transparent window for the cursor effect, flying from control to control
private static Bitmap _bmp = null;
private static Bitmap _bmp;
private readonly int _loop = 12;
private Rectangle _start;
private Rectangle _end;
Expand Down Expand Up @@ -41,8 +36,8 @@ await Task.Run(() => {
for (int i = 0; i < _loop; ++i)
{
// Use log to avoid constant speed
int distance = (int)(Math.Log10((double)i + 1.0) * 1000.0);
int max = (int)(Math.Log10((double)_loop) * 1000.0);
int distance = (int)(Math.Log10(i + 1.0) * 1000.0);
int max = (int)(Math.Log10(_loop) * 1000.0);
_current.X = _start.X + (_end.X - _start.X) * distance / max;
_current.Y = _start.Y + (_end.Y - _start.Y) * distance / max;
_current.Width = _start.Width + (_end.Width - _start.Width) * distance / max;
Expand Down
3 changes: 0 additions & 3 deletions 99x8Edit/EditorControl.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _99x8Edit
Expand Down
45 changes: 19 additions & 26 deletions 99x8Edit/Export.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;

namespace _99x8Edit
{
Expand Down Expand Up @@ -80,20 +78,17 @@ internal enum MapType
RawCompressed_Map,
};
// Data to be exported, to be set by host
private byte[] _ptnGen = new byte[256 * 8]; // Pattern generator table
private byte[] _ptnClr = new byte[256 * 8]; // Pattern color table
private byte[] _nameTable = new byte[768]; // Sandbox(Pattern name table)
private byte[] _pltDat = { 0x00, 0x00, 0x00, 0x00, 0x11, 0x06, 0x33, 0x07,
0x17, 0x01, 0x27, 0x03, 0x51, 0x01, 0x27, 0x06,
0x71, 0x01, 0x73, 0x03, 0x61, 0x06, 0x64, 0x06,
0x11, 0x04, 0x65, 0x02, 0x55, 0x05, 0x77, 0x07}; // Palette [RB][xG][RB][xG][RB]...
private bool _isTMS9918 = false;
private byte[] _mapPattern = new byte[256 * 4]; // One pattern mede by four characters
private byte[,] _mapData = new byte[64, 64]; // Map data[x, y](0..255)
private Int32 _mapWidth = 64;
private Int32 _mapHeight = 64;
private byte[] _spriteGen = new byte[256 * 8]; // Sprite pattern generator table
private byte[] _spriteClr = new byte[64 * 16]; // Sprite color(mode2)
private readonly byte[] _ptnGen; // Pattern generator table
private readonly byte[] _ptnClr; // Pattern color table
private readonly byte[] _nameTable; // Sandbox(Pattern name table)
private readonly byte[] _pltDat;
private readonly bool _isTMS9918;
private readonly byte[] _mapPattern; // One pattern mede by four characters
private readonly byte[,] _mapData; // Map data[x, y](0..255)
private readonly Int32 _mapWidth;
private readonly Int32 _mapHeight;
private readonly byte[] _spriteGen; // Sprite pattern generator table
private readonly byte[] _spriteClr; // Sprite color(mode2)
//------------------------------------------------------------------------
// Initialize
public Export(byte[] pcggen,
Expand Down Expand Up @@ -129,7 +124,7 @@ internal void ExportPCG(PCGType type, string path)
using StreamWriter sr = new StreamWriter(path, false);
sr.WriteLine("#ifndef __PCGDAT_H__");
sr.WriteLine("#define __PCGDAT_H__");
string str = "";
string str;
if (!_isTMS9918)
{
sr.WriteLine("// Palette");
Expand Down Expand Up @@ -184,7 +179,7 @@ internal void ExportPCG(PCGType type, string path)
using StreamWriter sr = new StreamWriter(path, false);
sr.WriteLine("; PCG Data");
sr.WriteLine("; this export data is not tested");
string str = "";
string str;
if (!_isTMS9918)
{
sr.WriteLine("; Palette r8b8g8");
Expand Down Expand Up @@ -285,7 +280,7 @@ internal void ExportMap(MapType type, string path)
sr.WriteLine("#define __MAPDAT_H__");
sr.WriteLine($"#define MAP_W ({_mapWidth})");
sr.WriteLine($"#define MAP_H ({_mapHeight})");
string str = "\t";
string str;
sr.WriteLine("// Map patterns");
if (type == MapType.CHeader)
{
Expand Down Expand Up @@ -329,7 +324,7 @@ internal void ExportMap(MapType type, string path)
using StreamWriter sr = new StreamWriter(path, false);
sr.WriteLine("; Map Data");
sr.WriteLine("; this export data is not tested");
string str = "";
string str;
sr.WriteLine("mapwidth:");
sr.WriteLine($"\tdb\t{_mapWidth}");
sr.WriteLine("mapheight:");
Expand Down Expand Up @@ -441,7 +436,7 @@ internal void ExportSprites(SpriteType type, string path)
using StreamWriter sr = new StreamWriter(path, false);
sr.WriteLine("#ifndef __SPRITEDAT_H__");
sr.WriteLine("#define __SPRITEDAT_H__");
string str = "";
string str;
sr.WriteLine("// Sprite generator table");
if (type == SpriteType.CHeader)
{
Expand Down Expand Up @@ -478,9 +473,8 @@ internal void ExportSprites(SpriteType type, string path)
using StreamWriter sr = new StreamWriter(path, false);
sr.WriteLine("; Sprite Data");
sr.WriteLine("; this export data is not tested");
string str = "";
string str;
sr.WriteLine("; Sprite generator table");
str = "";
if (type == SpriteType.ASMData)
{
sr.WriteLine("sprgen:");
Expand All @@ -495,7 +489,6 @@ internal void ExportSprites(SpriteType type, string path)
if (!_isTMS9918)
{
sr.WriteLine("; Sprite color table");
str = "";
if (type == SpriteType.ASMData)
{
sr.WriteLine("sprclr:");
Expand Down Expand Up @@ -626,7 +619,7 @@ private byte[] CompressMapData()
src_row.Add(_mapData[i, y]);
}
CompressionBase encoder = Compression.Create(Compression.Type.RunLength);
byte[] comp = encoder.Encode(src_row.ToArray() as byte[]);
byte[] comp = encoder.Encode(src_row.ToArray());
comp_data.Add(comp);
offset += (ushort)comp.Length;
}
Expand All @@ -645,7 +638,7 @@ private byte[] CompressMapData()
ret.Add(comp_data[i][j]);
}
}
return ret.ToArray() as byte[];
return ret.ToArray();
}
}
}
3 changes: 0 additions & 3 deletions 99x8Edit/Filters.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace _99x8Edit
{
Expand Down
Loading

0 comments on commit 6013549

Please sign in to comment.