Skip to content

Commit

Permalink
Merge pull request #22 from hiroxpepe/develop
Browse files Browse the repository at this point in the history
refactor: #1 keeping codes clean.
  • Loading branch information
hiroxpepe authored Oct 17, 2022
2 parents 555f19a + 94162e7 commit 568d7b6
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 153 deletions.
2 changes: 1 addition & 1 deletion Meowziq.View/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ MethodInvoker resetDisplay() {
// inner Classes

/// <summary>
/// a front class for processing.
/// front class for processing.
/// </summary>
static class Facade {

Expand Down
9 changes: 0 additions & 9 deletions Meowziq/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,5 @@ public static int Int32(this char source) {
public static bool HasValue(this string source) {
return !(source is null || source.Equals(string.Empty));
}

/// <summary>
/// gets the length of the beat.
/// </summary>
public static int GetBeatLength(this string source) {
const int MEAS_TO_BEAT = 4;
int length = source.Replace("][", "@").Split('@').Select(x => x.Replace("[", string.Empty).Replace("]", string.Empty)).ToArray().Length;
return length * MEAS_TO_BEAT;
}
}
}
94 changes: 37 additions & 57 deletions Meowziq/IO/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,89 +14,95 @@
*/

using System.IO;
using System.Text;

namespace Meowziq.IO {
/// <summary>
/// キャッシュ クラス
/// cache class
/// </summary>
/// <author>h.adachi (STUDIO MeowToon)</author>
public static class Cache {

///////////////////////////////////////////////////////////////////////////////////////////////
// static Fields

static Resourse _current; // この tick で読み込まれた json データの内容を保持

static Resourse _valid; // 全てのバリデーションを通過した json データの内容を保持
static Resourse _current_resourse, _valid_resourse;

///////////////////////////////////////////////////////////////////////////////////////////////
// static Constructor

static Cache() {
_current = new();
_valid = new();
_current_resourse = new();
_valid_resourse = new();
}

///////////////////////////////////////////////////////////////////////////////////////////////
// static Properties [noun, adjective]

/// <summary>
/// holds the contents of the json data read in this tick.
/// </summary>
public static Resourse Current {
get => _current;
get => _current_resourse;
}

/// <summary>
/// holds the final contents of json data that passed all validations.
/// </summary>
public static Resourse Valid {
get => _valid;
get => _valid_resourse;
}

///////////////////////////////////////////////////////////////////////////////////////////////
// public static Methods [verb]

/// <summary>
/// json ファイルを文字列として読み込みます
/// reads json files as strings.
/// </summary>
public static void Load(string targetPath) {
using var stream1 = new StreamReader($"{targetPath}/pattern.json");
_current.Pattern = stream1.ReadToEnd();
using var stream2 = new StreamReader($"{targetPath}/song.json");
_current.Song = stream2.ReadToEnd();
using var stream3 = new StreamReader($"{targetPath}/phrase.json");
_current.Phrase = stream3.ReadToEnd();
using var stream4 = new StreamReader($"{targetPath}/player.json");
_current.Player = stream4.ReadToEnd();
using StreamReader stream1 = new($"{targetPath}/pattern.json");
_current_resourse.Pattern = stream1.ReadToEnd();
using StreamReader stream2 = new($"{targetPath}/song.json");
_current_resourse.Song = stream2.ReadToEnd();
using StreamReader stream3 = new($"{targetPath}/phrase.json");
_current_resourse.Phrase = stream3.ReadToEnd();
using StreamReader stream4 = new($"{targetPath}/player.json");
_current_resourse.Player = stream4.ReadToEnd();
if (File.Exists($"{targetPath}/mixer.json")) {
using var stream5 = new StreamReader($"{targetPath}/mixer.json");
_current.Mixer = stream5.ReadToEnd();
using StreamReader stream5 = new($"{targetPath}/mixer.json");
_current_resourse.Mixer = stream5.ReadToEnd();
}
}

/// <summary>
/// バリデーションが通過した最新の内容として更新します
/// updates as the latest resourse that has passed validation.
/// </summary>
public static void Update() {
_valid.Pattern = _current.Pattern;
_valid.Song = _current.Song;
_valid.Phrase = _current.Phrase;
_valid.Player = _current.Player;
_valid.Mixer = _current.Mixer;
_valid_resourse.Pattern = _current_resourse.Pattern;
_valid_resourse.Song = _current_resourse.Song;
_valid_resourse.Phrase = _current_resourse.Phrase;
_valid_resourse.Player = _current_resourse.Player;
_valid_resourse.Mixer = _current_resourse.Mixer;
}

/// <summary>
/// 内容を初期化します
/// initializes the resourses.
/// </summary>
public static void Clear() {
_current.Clear();
_valid.Clear();
_current_resourse.Clear();
_valid_resourse.Clear();
}

///////////////////////////////////////////////////////////////////////////////////////////////
// inner Classes

/// <summary>
/// json ファイルの内容保持用 クラス
/// class for holding the contents of json files as strings.
/// </summary>
public class Resourse {

///////////////////////////////////////////////////////////////////////////////////////////
// public Properties [noun, adjective]

public Stream PatternStream {
get => Pattern.ToMemoryStream();
}
Expand Down Expand Up @@ -144,34 +150,8 @@ internal string Mixer {
// internal Methods [verb]

internal void Clear() {
Pattern = null;
Song = null;
Phrase = null;
Player = null;
Mixer = null;
Pattern = Song = Phrase = Player = Mixer = null;
}
}
}

/// <summary>
/// IO系拡張メソッド
/// </summary>
public static class Extensions {

public static MemoryStream ToMemoryStream(this string source) {
return new MemoryStream(Encoding.UTF8.GetBytes(source));
}

public static bool Is(this string source, string target) { // is でいい?
return source == target;
}

public static void Set(this string source, string target) {
source = target; // FIXME: 値渡しなのでこれでは無理 ⇒ C# の仕様で出来ない
}

public static void Clear(this string source) {
source = string.Empty; // FIXME: 値渡しなのでこれでは無理 ⇒ C# の仕様で出来ない
}
}
}
38 changes: 38 additions & 0 deletions Meowziq/IO/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System.IO;
using System.Text;

namespace Meowziq.IO {
/// <summary>
/// IO related extension methods.
/// </summary>
internal static class Extensions {
/// <summary>
/// converts a string to a MemoryStream.
/// </summary>
internal static MemoryStream ToMemoryStream(this string source) {
return new MemoryStream(Encoding.UTF8.GetBytes(source));
}

/// <summary>
/// compares two strings.
/// </summary>
internal static bool Is(this string source, string target) {
return source == target;
}
}
}
4 changes: 2 additions & 2 deletions Meowziq/IO/IOUtils.cs → Meowziq/IO/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace Meowziq.IO {
/// <summary>
/// IO utils functions.
/// <author>h.adachi (STUDIO MeowToon)</author>
public static class IOUtils {
internal static class Utils {
/// <summary>
/// creates a directory if necessary.
/// </summary>
public static DirectoryInfo MakeDirectoryIfNecessary(string target) {
internal static DirectoryInfo MakeDirectoryIfNecessary(string target) {
string directory = Path.GetDirectoryName(target);
bool exists = Directory.Exists(directory);
if (!exists) {
Expand Down
33 changes: 33 additions & 0 deletions Meowziq/Loader/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System.Linq;

namespace Meowziq.Loader {
/// <summary>
/// loader extension methods.
/// </summary>
/// <author>h.adachi (STUDIO MeowToon)</author>
internal static class Extensions {
/// <summary>
/// gets the length of the beat.
/// </summary>
internal static int GetBeatLength(this string source) {
const int MEAS_TO_BEAT = 4;
int length = source.Replace("][", "@").Split('@').Select(x => x.Replace("[", string.Empty).Replace("]", string.Empty)).ToArray().Length;
return length * MEAS_TO_BEAT;
}
}
}
20 changes: 12 additions & 8 deletions Meowziq/Loader/MixerLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,36 @@ public static class MixerLoader<T> {
// public static Methods [verb]

/// <summary>
/// Mixer を作成します
/// creates a Mixer object.
/// </summary>
public static void Build(Stream target) {
if (target is null) {
return;
}
loadJson(target).Mixer.Fader.ToList().Select(x =>
if (target is null) { return; }
loadJson(target).Mixer.Fader.ToList().Select(selector: x =>
new Core.Mixer<T>.Fader() {
Type = x.Type,
Name = x.Name,
ProgramNum = drumInst(x.Inst) ? (int) DrumKit.Enum.Parse(x.Inst) : (int) Instrument.Enum.Parse(x.Inst),
ProgramNum = drumInst(target: x.Inst) ? (int) DrumKit.Enum.Parse(target: x.Inst) : (int) Instrument.Enum.Parse(target: x.Inst),
Vol = x.Vol,
Pan = Pan.Enum.Parse(x.Pan),
Pan = Pan.Enum.Parse(target: x.Pan),
Mute = x.Mute
}
).ToList().ForEach(x => Core.Mixer<T>.AddFader = x);
).ToList().ForEach(action: x => Core.Mixer<T>.AddFader = x);
}

///////////////////////////////////////////////////////////////////////////////////////////////
// private static Methods [verb]

/// <summary>
/// reads a .json file to the JSON object.
/// </summary>
static Json loadJson(Stream target) {
DataContractJsonSerializer serializer = new(typeof(Json));
return (Json) serializer.ReadObject(target);
}

/// <summary>
/// whether it is a drum instrument.
/// </summary>
static bool drumInst(string target) {
return (target.Equals("Standard") || target.Equals("Room") || target.Equals("Power") ||
target.Equals("Electronic") || target.Equals("Analog") || target.Equals("Jazz") ||
Expand Down
3 changes: 3 additions & 0 deletions Meowziq/Loader/PatternLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ static void getCountBeatLength(Pattern pattern) {
}
}

/// <summary>
/// reads a .json file to the JSON object.
/// </summary>
static Json loadJson(Stream target) {
DataContractJsonSerializer serializer = new(typeof(Json));
return (Json) serializer.ReadObject(target);
Expand Down
3 changes: 3 additions & 0 deletions Meowziq/Loader/PhraseLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ static Core.Phrase searchBasePhrase(string phrase_type, string phrase_name, List
}
}

/// <summary>
/// reads a .json file to the JSON object.
/// </summary>
static Json loadJson(Stream target) {
DataContractJsonSerializer serializer = new(typeof(Json));
return (Json) serializer.ReadObject(target);
Expand Down
7 changes: 4 additions & 3 deletions Meowziq/Loader/PlayerLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public static List<Phrase> PhraseList {
/// creates a list of players.
/// </summary>
public static List<Core.Player<T>> Build(Stream target) {
if (_phrase_list is null) {
throw new ArgumentException("need _phrase_list.");
}
if (_phrase_list is null) { throw new ArgumentException("need _phrase_list."); }
/// <remarks>
/// converts to a list of Core.Player.
/// </remarks>
Expand All @@ -73,6 +71,9 @@ static Core.Player<T> convertPlayer(Player player) {
return new_player;
}

/// <summary>
/// reads a .json file to the JSON object.
/// </summary>
static Json loadJson(Stream target) {
DataContractJsonSerializer serializer = new(typeof(Json));
return (Json) serializer.ReadObject(target);
Expand Down
9 changes: 5 additions & 4 deletions Meowziq/Loader/SongLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public static List<Pattern> PatternList {
/// creates a song.
/// </summary>
public static Core.Song Build(Stream target) {
if (_pattern_list is null) {
throw new ArgumentException("need _pattern_list.");
}
if (_pattern_list is null) { throw new ArgumentException("need _pattern_list."); }
Song song = loadJson(target).Song;
List<Core.Section> section_list = song.Section.Select(x =>
new Core.Section(
Expand All @@ -73,12 +71,15 @@ static Pattern searchPattern(string pattern_name) {
/// <remarks>
/// first element with matching name.
/// </remarks>
return _pattern_list.Where(x => x.Name.Equals(pattern_name)).First();
return _pattern_list.Where(predicate: x => x.Name.Equals(pattern_name)).First();
} catch {
throw new ArgumentException("undefined pattern.");
}
}

/// <summary>
/// reads a .json file to the JSON object.
/// </summary>
static Json loadJson(Stream target) {
DataContractJsonSerializer serializer = new(typeof(Json));
return (Json) serializer.ReadObject(target);
Expand Down
Loading

0 comments on commit 568d7b6

Please sign in to comment.