Skip to content

Commit 9feb096

Browse files
authored
Merge pull request #783 from CnCNet/develop
Release 2.12.6
2 parents 87ecde9 + 1e8ddf7 commit 9feb096

25 files changed

+501
-172
lines changed

ClientCore/Enums/AllowPrivateMessagesFromEnum.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
public enum AllowPrivateMessagesFromEnum
44
{
55
All = 1,
6+
CurrentChannel = 4,
67
Friends = 2,
7-
None = 3
8+
None = 3,
89
}
910
}

ClientCore/Extensions/StringExtensions.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Text.RegularExpressions;
23
using System.Collections.Generic;
34

45
using ClientCore.I18N;
@@ -7,22 +8,23 @@ namespace ClientCore.Extensions;
78

89
public static class StringExtensions
910
{
10-
public static string GetLink(this string text)
11+
private static Regex extractLinksRE = new Regex(@"((http[s]?)|(ftp))\S+");
12+
13+
public static string[] GetLinks(this string text)
1114
{
1215
if (string.IsNullOrWhiteSpace(text))
1316
return null;
1417

15-
int index = text.IndexOf("http://", StringComparison.Ordinal);
16-
if (index == -1)
17-
index = text.IndexOf("ftp://", StringComparison.Ordinal);
18-
if (index == -1)
19-
index = text.IndexOf("https://", StringComparison.Ordinal);
18+
var matches = extractLinksRE.Matches(text);
2019

21-
if (index == -1)
20+
if (matches.Count == 0)
2221
return null; // No link found
2322

24-
string link = text.Substring(index);
25-
return link.Split(' ')[0]; // Nuke any words coming after the link
23+
string[] links = new string[matches.Count];
24+
for (int i = 0; i < links.Length; i++)
25+
links[i] = matches[i].Value.Trim();
26+
27+
return links;
2628
}
2729

2830
private const string ESCAPED_INI_NEWLINE_PATTERN = $"\\{ProgramConstants.INI_NEWLINE_PATTERN}";
@@ -113,4 +115,4 @@ public static string ToWin32FileName(this string filename)
113115

114116
public static T ToEnum<T>(this string value) where T : Enum
115117
=> (T)Enum.Parse(typeof(T), value, true);
116-
}
118+
}

ClientCore/I18N/Translation.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
using System.Globalization;
44
using System.IO;
55
using System.Linq;
6+
using System.Text;
7+
68
using ClientCore.Extensions;
9+
using ClientCore.PlatformShim;
10+
711
using Rampastring.Tools;
812
using Rampastring.XNAUI.XNAControls;
913

@@ -50,6 +54,9 @@ public CultureInfo Culture
5054
/// <summary>The author(s) of the translation.</summary>
5155
public string Author { get; private set; } = string.Empty;
5256

57+
/// <summary>Override the default encoding used for reading/writing map files. Null ("Auto") means detecting the encoding from each file (sometimes unreliable). </summary>
58+
public Encoding MapEncoding = EncodingExt.UTF8NoBOM;
59+
5360
/// <summary>Stores the translation values (including default values for missing strings).</summary>
5461
private Dictionary<string, string> Values { get; } = new();
5562

@@ -95,6 +102,8 @@ public Translation(IniFile ini, string localeCode)
95102
Name = metadataSection?.GetStringValue(nameof(Name), string.Empty);
96103
Author = metadataSection?.GetStringValue(nameof(Author), string.Empty);
97104

105+
MapEncoding = EncodingExt.GetEncodingWithAuto(metadataSection?.GetStringValue(nameof(MapEncoding), null));
106+
98107
string cultureName = metadataSection?.GetStringValue(nameof(Culture), null);
99108
if (cultureName is not null)
100109
Culture = new(cultureName);
@@ -122,6 +131,7 @@ public Translation(Translation other)
122131
_name = other._name;
123132
_culture = other._culture;
124133
Author = other.Author;
134+
MapEncoding = other.MapEncoding;
125135

126136
foreach (var (key, value) in other.Values)
127137
Values.Add(key, value);
@@ -251,6 +261,8 @@ public IniFile DumpIni(bool saveOnlyMissingValues = false)
251261

252262
general.AddKey(nameof(Author), Author);
253263

264+
general.AddKey(nameof(MapEncoding), EncodingExt.EncodingWithAutoToString(MapEncoding));
265+
254266
ini.AddSection(nameof(Values));
255267
IniSection translation = ini.GetSection(nameof(Values));
256268

ClientCore/PlatformShim/EncodingExt.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#nullable enable
12
using System.Text;
23

34
namespace ClientCore.PlatformShim;
@@ -18,4 +19,32 @@ static EncodingExt()
1819
public static Encoding ANSI { get; }
1920

2021
public static Encoding UTF8NoBOM { get; } = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
22+
23+
public const string ENCODING_AUTO_DETECT = "Auto";
24+
25+
public static Encoding? GetEncodingWithAuto(string? encodingName)
26+
{
27+
if (encodingName is null)
28+
return UTF8NoBOM;
29+
30+
if (encodingName.Equals(ENCODING_AUTO_DETECT, System.StringComparison.InvariantCultureIgnoreCase))
31+
return null;
32+
33+
Encoding encoding = Encoding.GetEncoding(encodingName);
34+
35+
// We don't expect UTF-8 BOM for the string "UTF-8"
36+
if (encoding is UTF8Encoding)
37+
encoding = UTF8NoBOM;
38+
39+
return encoding;
40+
}
41+
42+
public static string EncodingWithAutoToString(Encoding? encoding)
43+
{
44+
if (encoding is null)
45+
return ENCODING_AUTO_DETECT;
46+
47+
// To find a name that can be passed to the GetEncoding method, use the WebName property.
48+
return encoding.WebName;
49+
}
2150
}

ClientCore/ProfanityFilter.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Text.RegularExpressions;
44

@@ -27,27 +27,53 @@ public ProfanityFilter()
2727
"cock",
2828
"pussy",
2929
"hitler*",
30-
"anal"
30+
"anal",
31+
"dick",
32+
"faggot",
33+
"whore",
34+
"slut",
35+
"motherfucker",
36+
"asshole",
37+
"bitch",
38+
"bastard",
39+
"kike",
40+
"chink",
41+
"spic",
42+
"retard",
43+
"tranny",
44+
"jizz",
45+
"gangbang",
46+
"handjob",
47+
"blowjob",
48+
"rimjob",
49+
"porn",
50+
"rape",
51+
"rapist",
52+
"molest",
53+
"incest",
54+
"bestiality",
55+
"zoophile",
56+
"zoophilia",
57+
"chingchong",
58+
"slanty",
59+
"zipperhead",
60+
"gook",
3161
};
3262
}
3363

3464
public ProfanityFilter(IEnumerable<string> censoredWords)
3565
{
3666
if (censoredWords == null)
37-
throw new ArgumentNullException("censoredWords");
67+
throw new ArgumentNullException(nameof(censoredWords));
3868
CensoredWords = new List<string>(censoredWords);
3969
}
4070

4171
public bool IsOffensive(string text)
4272
{
43-
string censoredText = text;
4473
foreach (string censoredWord in CensoredWords)
4574
{
4675
string regularExpression = ToRegexPattern(censoredWord);
47-
censoredText = Regex.Replace(censoredText, regularExpression, "",
48-
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
49-
50-
if(string.IsNullOrEmpty(censoredText))
76+
if (Regex.IsMatch(text, regularExpression, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
5177
return true;
5278
}
5379
return false;
@@ -56,7 +82,7 @@ public bool IsOffensive(string text)
5682
public string CensorText(string text)
5783
{
5884
if (text == null)
59-
throw new ArgumentNullException("text");
85+
throw new ArgumentNullException(nameof(text));
6086
string censoredText = text;
6187
foreach (string censoredWord in CensoredWords)
6288
{

ClientGUI/XNAClientColorDropDown.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using ClientCore.Extensions;
2+
using Microsoft.Xna.Framework;
3+
using Microsoft.Xna.Framework.Graphics;
4+
5+
using Rampastring.Tools;
6+
using Rampastring.XNAUI;
7+
using Rampastring.XNAUI.XNAControls;
8+
9+
namespace ClientGUI
10+
{
11+
public class XNAClientColorDropDown : XNAClientDropDown
12+
{
13+
private const int VERTICAL_PADDING = 3;
14+
private const int HORIZONTAL_PADDING = 2;
15+
public ItemsKind ItemsDrawMode { get; private set; } = ItemsKind.TextAndIcon;
16+
17+
public int ColorTextureWidth { get; private set; }
18+
public int ColorTextureHeight { get; private set; }
19+
public Texture2D RandomColorTexture { get; private set; }
20+
public Texture2D DisabledItemTexture { get; private set; }
21+
22+
public XNAClientColorDropDown(WindowManager windowManager) : base(windowManager)
23+
{
24+
ColorTextureWidth = Height - VERTICAL_PADDING;
25+
ColorTextureHeight = Height - HORIZONTAL_PADDING;
26+
RandomColorTexture = AssetLoader.LoadTexture("randomicon.png");
27+
DisabledItemTexture = AssetLoader.CreateTexture(DisabledItemColor, ColorTextureWidth, ColorTextureHeight);
28+
}
29+
30+
protected override void ParseControlINIAttribute(IniFile iniFile, string key, string value)
31+
{
32+
switch (key)
33+
{
34+
case nameof(ItemsDrawMode):
35+
ItemsDrawMode = value.FromIniString().ToEnum<ItemsKind>();
36+
37+
switch (ItemsDrawMode)
38+
{
39+
case ItemsKind.Text:
40+
Items.ForEach(item =>
41+
{
42+
// Disposing cause client to crash, so replace texture with transparent one
43+
item.Texture = AssetLoader.CreateTexture(AssetLoader.GetRGBAColorFromString("0,0,0,0"), 1, 1);
44+
});
45+
break;
46+
case ItemsKind.Icon:
47+
ColorTextureWidth = Width - VERTICAL_PADDING;
48+
ColorTextureHeight = Height - HORIZONTAL_PADDING;
49+
50+
Items.ForEach(item =>
51+
{
52+
if (Items[0] != item)
53+
item.Texture = AssetLoader.CreateTexture(
54+
item.TextColor ?? Color.White,
55+
ColorTextureWidth,
56+
ColorTextureHeight);
57+
58+
item.Text = string.Empty;
59+
});
60+
61+
DisabledItemTexture = AssetLoader.CreateTexture(DisabledItemColor, Width - VERTICAL_PADDING, Height - HORIZONTAL_PADDING);
62+
63+
break;
64+
case ItemsKind.TextAndIcon:
65+
break;
66+
default:
67+
break;
68+
}
69+
70+
return;
71+
case nameof(ColorTextureWidth):
72+
ColorTextureWidth = Conversions.IntFromString(value, ColorTextureWidth);
73+
break;
74+
case nameof(ColorTextureHeight):
75+
ColorTextureHeight = Conversions.IntFromString(value, ColorTextureHeight);
76+
break;
77+
case nameof(RandomColorTexture):
78+
RandomColorTexture = AssetLoader.LoadTexture(value);
79+
Items[0].Texture = RandomColorTexture;
80+
break;
81+
default:
82+
base.ParseControlINIAttribute(iniFile, key, value);
83+
return;
84+
}
85+
}
86+
87+
public new virtual void AddItem(string text, Color color)
88+
{
89+
var item = new XNADropDownItem();
90+
91+
item.Text = text;
92+
item.TextColor = color;
93+
94+
if (Items.Count > 1)
95+
item.Texture = AssetLoader.CreateTexture(color, ColorTextureWidth, ColorTextureHeight);
96+
else
97+
item.Texture = RandomColorTexture;
98+
99+
Items.Add(item);
100+
}
101+
102+
public enum ItemsKind
103+
{
104+
Text,
105+
Icon,
106+
TextAndIcon
107+
}
108+
}
109+
}

DTAConfig/OptionPanels/CnCNetOptionsPanel.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,30 @@ private void InitAllowPrivateMessagesFromDropdown()
188188
ddAllowPrivateMessagesFrom.Name = nameof(ddAllowPrivateMessagesFrom);
189189
ddAllowPrivateMessagesFrom.ClientRectangle = new Rectangle(
190190
lblAllPrivateMessagesFrom.Right,
191-
lblAllPrivateMessagesFrom.Y - 2, 65, 0);
191+
lblAllPrivateMessagesFrom.Y - 2, 110, 0);
192192

193193
ddAllowPrivateMessagesFrom.AddItem(new XNADropDownItem()
194194
{
195195
Text = "All".L10N("Client:DTAConfig:PMAll"),
196-
Tag = AllowPrivateMessagesFromEnum.All
196+
Tag = AllowPrivateMessagesFromEnum.All,
197+
});
198+
199+
ddAllowPrivateMessagesFrom.AddItem(new XNADropDownItem()
200+
{
201+
Text = "Current channel".L10N("Client:DTAConfig:PMCurrentChannel"),
202+
Tag = AllowPrivateMessagesFromEnum.CurrentChannel,
197203
});
198204

199205
ddAllowPrivateMessagesFrom.AddItem(new XNADropDownItem()
200206
{
201207
Text = "Friends".L10N("Client:DTAConfig:PMFriends"),
202-
Tag = AllowPrivateMessagesFromEnum.Friends
208+
Tag = AllowPrivateMessagesFromEnum.Friends,
203209
});
204210

205211
ddAllowPrivateMessagesFrom.AddItem(new XNADropDownItem()
206212
{
207213
Text = "None".L10N("Client:DTAConfig:PMNone"),
208-
Tag = AllowPrivateMessagesFromEnum.None
214+
Tag = AllowPrivateMessagesFromEnum.None,
209215
});
210216

211217
AddChild(ddAllowPrivateMessagesFrom);

DXMainClient/DXGUI/GameClass.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ protected override void Initialize()
148148

149149
wm.ControlINIAttributeParsers.Add(new TranslationINIParser());
150150

151-
MainClientConstants.DisplayErrorAction = (title, error, exit) =>
152-
{
153-
new XNAMessageBox(wm, title, error, XNAMessageBoxButtons.OK)
154-
{
155-
OKClickedAction = _ =>
156-
{
157-
if (exit)
158-
Environment.Exit(1);
159-
}
160-
}.Show();
161-
};
162-
163151
SetGraphicsMode(wm);
164152

165153
#if WINFORMS

0 commit comments

Comments
 (0)