Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
wjg999 committed Sep 1, 2020
2 parents 3293aa7 + 189fbca commit 26199e8
Show file tree
Hide file tree
Showing 25 changed files with 1,078 additions and 875 deletions.
70 changes: 20 additions & 50 deletions Randomizer/Logic/DirectiveParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,16 @@ private LogicColorPicker ParseColorDirective(string[] directiveParts)
for (int i = 4; i < directiveParts.Length; i += 3)
{
// This parses the color components out, in groups of three.
if (int.TryParse(directiveParts[i], NumberStyles.HexNumber, null, out int rComponent))
if (StringUtil.ParseString(directiveParts[i], out int rComponent) &&
StringUtil.ParseString(directiveParts[i + 1], out int gComponent) &&
StringUtil.ParseString(directiveParts[i + 2], out int bComponent)
)
{
if (int.TryParse(directiveParts[i + 1], NumberStyles.HexNumber, null, out int gComponent))
{
if (int.TryParse(directiveParts[i + 2], NumberStyles.HexNumber, null, out int bComponent))
{
colors.Add(Color.FromArgb(rComponent, gComponent, bComponent));
}
}
colors.Add(Color.FromArgb(rComponent, gComponent, bComponent));
}
else
{
throw new ParserException($"A color somewhere has an incorrect number! ({directiveParts[i]} - {directiveParts[i+1]} - {directiveParts[i+2]})");
}
}

Expand Down Expand Up @@ -524,9 +525,9 @@ private LogicOptionType GetOptionType(string typeString)

private uint ParseCrcDirective(string[] directiveParts)
{
if (uint.TryParse(directiveParts[1], NumberStyles.HexNumber, null, out uint outCrc))
if (StringUtil.ParseString(directiveParts[1], out int outCrc))
{
return outCrc;
return (uint)outCrc;
}
else
{
Expand All @@ -541,48 +542,20 @@ private void ParseReplaceChanceDirective(string[] directiveParts)
throw new ParserException("!replace has an invalid amount of arguments");
}

var itemData = directiveParts[1].Split(':');
var itemStrings = itemData[0].Split('.');
var chanceItems = directiveParts[2].Split(',');

Item replacedItem;
var chanceItemList = new List<ChanceItem>();

ItemType type;
byte itemsub = 0;

if (!Enum.TryParse(itemStrings[1], out type))
{
throw new ParserException("!replace has an invalid replaced itemType");
}


if (itemStrings.Length >= 3)
{
if (!byte.TryParse(itemStrings[2], NumberStyles.HexNumber, null, out itemsub))
{
throw new ParserException("!replace has an invalid replaced itemSub");
}
}

var dungeonString = "";
if (itemData.Length > 1)
{
dungeonString = itemData[1];
}

replacedItem = new Item(directiveParts[1], "!replace");

var chanceItems = directiveParts[2].Split(',');
foreach (var chanceItem in chanceItems)
{
var trimmed = chanceItem.TrimEnd(';');
var chanceItemData = trimmed.Split(':');
var chanceItemStrings = chanceItemData[0].Split('.');



int chance = 0;
if (!int.TryParse(chanceItemData[2], out chance))
if (!StringUtil.ParseString(chanceItemData[2], out chance))
{
throw new ParserException("!replace has an invalid new item chance value");
}
Expand All @@ -603,15 +576,14 @@ private void ParseReplaceAmountDirective(string[] directiveParts)
var replacementItem = new Item(directiveParts[1], "!replaceamount");

byte replacementAmount;
if (!byte.TryParse(directiveParts[2], NumberStyles.HexNumber, null, out replacementAmount))
if (!StringUtil.ParseString(directiveParts[2], out replacementAmount))
{
throw new ParserException("!replaceamount has an invalid amount");
}

AmountReplacementReferences.Add(new ItemAmountSet(replacementItem, replacementAmount));
var referenceId = AmountReplacementReferences.Count - 1;

var itemList = new List<Item>();
var replacedItems = directiveParts[3].Split(',');
foreach (var itemString in replacedItems)
{
Expand Down Expand Up @@ -643,15 +615,14 @@ private void ParseReplaceIncrementDirective(string[] directiveParts)
var replacementItem = new Item(directiveParts[1], "!replaceincrement");

byte replacementAmount;
if (!byte.TryParse(directiveParts[2], NumberStyles.HexNumber, null, out replacementAmount))
if (!StringUtil.ParseString(directiveParts[2], out replacementAmount))
{
throw new ParserException("!replaceincrement has an invalid amount");
}

IncrementalReplacementReferences.Add(new ItemAmountSet(replacementItem, replacementAmount));
var referenceId = IncrementalReplacementReferences.Count - 1;

var itemList = new List<Item>();
var replacedItems = directiveParts[3].Split(',');
foreach (var itemString in replacedItems)
{
Expand All @@ -673,26 +644,25 @@ private void ParseReplaceIncrementDirective(string[] directiveParts)
}
}

public LogicDefine ParseAdditionDirective(String[] directiveParts)
public LogicDefine ParseAdditionDirective(string[] directiveParts)
{
var name = directiveParts[1];
int totalValue = 0;
var values = directiveParts[2].Split(',');
foreach(String value in values)
foreach(string value in values)
{
byte number;
if (!byte.TryParse(value, out number))
if (!StringUtil.ParseString(value, out number))
{
throw new ParserException("!addition has an invalid value");
}
totalValue += number;

if(totalValue>255)
if(totalValue > 255)
{
throw new ParserException("!addition resulted in a value higher than 255 (0xFF)");
}
}
return new LogicDefine( directiveParts[1] ,totalValue.ToString("X2"));
return new LogicDefine( directiveParts[1] ,totalValue.ToString());
}

public void ParseSetTypeDirective(string[] directiveParts)
Expand Down
55 changes: 23 additions & 32 deletions Randomizer/Logic/LogicOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,85 +213,76 @@ public override byte GetHashByte()

public class LogicNumberBox : LogicOption
{
String value = "";
String placeholder = "";
Boolean focussing = false;
public LogicNumberBox(string name, LogicOptionType type, String placeholder) : base(name, name, true, type)
string value = "";
string placeholder = "";
bool isSelf = false;
public LogicNumberBox(string name, LogicOptionType type, string placeholder) : base(name, name, true, type)
{
this.placeholder = placeholder;
}

public override Control GetControl()
{
TextBox numberBox = new TextBox();
focussing = true;
isSelf = true;
numberBox.Text = placeholder;
focussing = false;
isSelf = false;
numberBox.LostFocus += (object sender, EventArgs e) => { LostFocus(sender, e); };
numberBox.GotFocus += (object sender, EventArgs e) => { GotFocus(sender, e); };

numberBox.TextChanged += (object sender, EventArgs e) => { TextChanged(sender, e); };

return numberBox;
}

private void LostFocus(object sender, EventArgs e)
{
if (value == "")
{
focussing = true;
isSelf = true;
((TextBox)sender).Text = placeholder;
focussing = false;
isSelf = false;
}
}

private void GotFocus(object sender, EventArgs e)
{
if(value == "")
{
focussing = true;
isSelf = true;
((TextBox)sender).Text = "";
focussing = false;
isSelf = false;
}
}

private void TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
byte val;
if(focussing)
if (isSelf)
{
return;
}
var tb = ((TextBox)sender);
var text = tb.Text;
byte val;

if(tb.Text == ""||tb.Text == "0" || tb.Text == "0x" || tb.Text == "0X")
text = text.TrimStart(new char[] { ' ', '0' });
if(text == "")
{
value = "";
return;
}

//dont self trigger, if thats a thing
focussing = true;
if (tb.Text.StartsWith("0x") || tb.Text.StartsWith("0X"))
{
if (byte.TryParse(tb.Text.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out val))
{
value = val.ToString();
}
else
{ //starts with 0X so give a hex representation
tb.Text = value != "" ? "0x" +value : "";
}
}
else if (byte.TryParse(tb.Text, out val))
if (byte.TryParse(text, out val))
{
value = val.ToString();
}
else
{ //doesnt start with 0X so give a decimal representation
tb.Text = value != "" ? byte.Parse(value,NumberStyles.HexNumber,CultureInfo.InvariantCulture).ToString() : "";
{
var start = tb.SelectionStart;
isSelf = true;
tb.Text = value;
tb.Select(start - 1, 0);
isSelf = false;
}
focussing = false;
}

public override List<LogicDefine> GetLogicDefines()
Expand Down
Loading

0 comments on commit 26199e8

Please sign in to comment.