• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

FreeTrainの進化系を目指す


Commit MetaInfo

Revision7 (tree)
Time2013-12-30 19:03:02
Authorc477

Log Message

例外処理の改善と条件付リソース(オーバーライド)の基本インターフェース

Change Summary

Incremental Difference

--- core/core/view/ISceneCondition.cs (nonexistent)
+++ core/core/view/ISceneCondition.cs (revision 7)
@@ -0,0 +1,34 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Text;
4+using nft.framework.drawing;
5+using nft.framework;
6+
7+namespace nft.core.view
8+{
9+ /// <summary>
10+ /// Categorize type of conditions by its watch parameter and time cycle.
11+ /// Array of same type conditions can be combined and optimized as Hashmap by ConditionResourceFactory
12+ /// </summary>
13+ public enum ConditionTypes { Unknown, TimeOfDay, Daily, Weekly, Monthly, Yearly, Weather, Scale }
14+
15+ public interface ISceneParams
16+ {
17+ DateTime Datetime { get; }
18+ Object Weather { get; }
19+ ZoomScale ZoomScale { get; }
20+ }
21+
22+ public interface IConditionFactory
23+ {
24+ ISceneCondition CreateCondition(ParamsReader reader);
25+ }
26+
27+ public interface ISceneCondition
28+ {
29+ string ConditionName { get; }
30+ ConditionTypes ConditionType { get; }
31+ bool IsMatch(ISceneParams p);
32+ }
33+
34+}
--- core/core/view/ConditiondResource.cs (nonexistent)
+++ core/core/view/ConditiondResource.cs (revision 7)
@@ -0,0 +1,85 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Text;
4+
5+namespace nft.core.view
6+{
7+ public class ConditionedResource<T>
8+ {
9+ internal protected T Default;
10+
11+ public ConditionedResource(T _default) {
12+ this.Default = _default;
13+ }
14+
15+ public T this[ISceneParams p] {
16+ get {
17+ T result = this.Default;
18+ Choose(p, ref result);
19+ return result;
20+ }
21+ }
22+
23+ internal protected virtual bool Choose(ISceneParams p, ref T res){
24+ res = Default;
25+ return true;
26+ }
27+ }
28+
29+ internal class SingleConditionResouce<T> : ConditionedResource<T>
30+ {
31+ protected readonly ISceneCondition Condition;
32+ internal SingleConditionResouce(ISceneCondition cond, T val) :base(val){
33+ this.Condition = cond;
34+ }
35+
36+ internal protected override bool Choose(ISceneParams p, ref T res) {
37+ if (Condition.IsMatch(p)) {
38+ res = Default;
39+ return true;
40+ } else {
41+ return false;
42+ }
43+ }
44+ }
45+
46+ internal class NestedConditionsResouceRoot<T> : ConditionedResource<T>
47+ {
48+ internal protected readonly List<ConditionedResource<T>> children;
49+ internal NestedConditionsResouceRoot(T val)
50+ : base(val) {
51+ this.children = new List<ConditionedResource<T>>();
52+ }
53+
54+ internal protected override bool Choose(ISceneParams p, ref T res) {
55+ foreach (ConditionedResource<T> cc in children) {
56+ if (cc.Choose(p, ref res)) {
57+ return true;
58+ }
59+ }
60+ res = Default;
61+ return true;
62+ }
63+
64+ public void AddChild(ConditionedResource<T> child) {
65+ children.Add(child);
66+ }
67+ }
68+
69+ internal class NestedConditionsResouce<T> : NestedConditionsResouceRoot<T>
70+ {
71+ internal protected readonly ISceneCondition Condition;
72+ internal NestedConditionsResouce(ISceneCondition cond, T val)
73+ : base(val) {
74+ this.Condition = cond;
75+ }
76+
77+ internal protected override bool Choose(ISceneParams p, ref T res) {
78+ if (Condition.IsMatch(p)) {
79+ return base.Choose(p, ref res);
80+ } else {
81+ return false;
82+ }
83+ }
84+ }
85+}
--- core/core/view/ConditiondResourceFactory.cs (nonexistent)
+++ core/core/view/ConditiondResourceFactory.cs (revision 7)
@@ -0,0 +1,71 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Text;
4+using nft.framework.drawing;
5+using nft.framework;
6+using nft.framework.plugin;
7+
8+namespace nft.core.view
9+{
10+ public delegate T ResourceParser<T>(ParamsReader reader);
11+
12+ public class ConditionedResourceFactory
13+ {
14+ static private readonly string VALUE_NODE = "default|value";
15+ static private readonly string CONDITION_NODE = "condition";
16+ static private readonly string CONDITION_TYPE_ATTR = "when";
17+
18+ static private Dictionary<string, IConditionFactory> factories = new Dictionary<string, IConditionFactory>();
19+
20+ public static void RegisterConditionFactory(string name, IConditionFactory fct){
21+ factories.Add(name, fct);
22+ }
23+
24+ public static ConditionedResource<T> LoadAsConditiondResources<T>(ParamsReader resource, ResourceParser<T> callback){
25+ ParamsReader dnode = resource[VALUE_NODE];
26+ T _default = callback(dnode.IsNull ? resource : dnode);
27+ ParamsReader conds = resource[CONDITION_NODE];
28+ if (conds.IsNull) {
29+ return new ConditionedResource<T>(_default);
30+ } else {
31+ NestedConditionsResouceRoot<T> ret = new NestedConditionsResouceRoot<T>(_default);
32+ foreach (ParamsReader child in conds.Each) {
33+ ConditionedResource<T> temp = LoadChildConditions(resource, callback);
34+ ret.AddChild(temp);
35+ }
36+ return AdaptOptimization<T>(ret);
37+ }
38+ }
39+
40+ #region private functions
41+ private static ConditionedResource<T> LoadChildConditions<T>(ParamsReader resource, ResourceParser<T> callback){
42+ ISceneCondition sc = GetConditionFor(resource);
43+ ParamsReader dnode = resource[VALUE_NODE];
44+ T _default = callback(dnode.IsNull ? resource : dnode);
45+ ParamsReader conds = resource[CONDITION_NODE];
46+ if (conds.IsNull) {
47+ return new SingleConditionResouce<T>(sc,_default);
48+ } else {
49+ NestedConditionsResouce<T> ret = new NestedConditionsResouce<T>(sc, _default);
50+ foreach (ParamsReader child in conds.Each) {
51+ ConditionedResource<T> temp = LoadChildConditions(resource, callback);
52+ ret.AddChild(temp);
53+ }
54+ return AdaptOptimization<T>(ret);
55+ }
56+ }
57+
58+ private static ConditionedResource<T> AdaptOptimization<T>(NestedConditionsResouceRoot<T> orig) {
59+ return orig;
60+ }
61+
62+ private static ISceneCondition GetConditionFor(ParamsReader reader) {
63+ ParamsReader pr2 = reader[CONDITION_TYPE_ATTR];
64+ if (pr2.IsNull) throw new PluginXmlException(reader, "");
65+ string attr = pr2.InnerText;
66+
67+ return null;
68+ }
69+ #endregion
70+ }
71+}
--- framework/nftfw.resource.xml (revision 6)
+++ framework/nftfw.resource.xml (revision 7)
@@ -68,6 +68,9 @@
6868 <property name="type_indexed_grayscale" type="string" readonly="true">{1}色グレー</property>
6969 <property name="type_grayscale" type="string" readonly="true">{0}bit グレー</property>
7070 </properties>
71+ <properties name="ui_util">
72+ <property name="confirm_exit_message" type="string" readonly="true">NeoFTを終了してよろしいですか?</property>
73+ </properties>
7174 <properties name="mainframe">
7275 <property name="long_title" type="string" readonly="true">Neo Free Train</property>
7376 <property name="short_title" type="string" readonly="true">NeoFT</property>
--- framework/framework/Directories.cs (revision 6)
+++ framework/framework/Directories.cs (revision 7)
@@ -81,7 +81,8 @@
8181 catch(Exception e)
8282 {
8383 string templ = Main.resources["directories.errormsg_on_dir_creation"].stringValue;
84- UIUtil.ShowException(string.Format(templ,path),e,UIInformLevel.normal);
84+ if(UIUtil.ShowException(string.Format(templ,path),e,UIInformLevel.normal))
85+ throw;
8586 }
8687 return defaultPath;
8788 }
--- framework/framework/GlobalModules.cs (revision 6)
+++ framework/framework/GlobalModules.cs (revision 7)
@@ -33,7 +33,8 @@
3333 }
3434 }catch(Exception e){
3535 string txt = Main.resources["global_modules.load_error"].stringValue;
36- UIUtil.ShowException(txt,e,UIInformLevel.severe);
36+ if(UIUtil.ShowException(txt,e,UIInformLevel.severe))
37+ throw;
3738 }
3839 }
3940 }
--- framework/framework/plugin/PluginManager.cs (revision 6)
+++ framework/framework/plugin/PluginManager.cs (revision 7)
@@ -145,7 +145,8 @@
145145
146146 string templ = Main.resources["plugin.plugin_load_error"].stringValue;
147147 templ+="\n"+e.Message;
148- ReportError(string.Format(templ,Path.GetFileName(dir)),e);
148+ if(ReportError(string.Format(templ,Path.GetFileName(dir)),e))
149+ throw;
149150 }
150151 }
151152 }
@@ -193,7 +194,8 @@
193194 }
194195 catch( Exception e )
195196 {
196- ReportError(e.Message,e);
197+ if(ReportError(e.Message,e))
198+ throw;
197199 }
198200 // Add 'p' to order-list and remove from remaining-list
199201 pluginSet.Remove(p);
@@ -217,7 +219,8 @@
217219 }
218220 catch( Exception e )
219221 {
220- ReportError(e.Message,e);
222+ if(ReportError(e.Message,e))
223+ throw;
221224 }
222225 }
223226
@@ -232,7 +235,8 @@
232235 catch( Exception e )
233236 {
234237 p._state = InstallationState.FatalError;
235- ReportError(e.Message,e);
238+ if(ReportError(e.Message,e))
239+ throw;
236240 }
237241 }
238242 }
@@ -261,7 +265,8 @@
261265 } catch (Exception e) {
262266 contrib._state = InstallationState.FatalError;
263267 string templ = Main.resources["plugin.contrib_init_error"].stringValue;
264- ReportError(string.Format(templ, contrib.Parent.ID, "contrib.name", contrib.ID), e);
268+ if(ReportError(string.Format(templ, contrib.Parent.ID, "contrib.name", contrib.ID), e))
269+ throw;
265270 }
266271 }
267272 foreach (Contribution contrib in nonPrimitives)
@@ -281,7 +286,8 @@
281286 {
282287 contrib._state = InstallationState.FatalError;
283288 string templ = Main.resources["plugin.contrib_init_error"].stringValue;
284- ReportError(string.Format(templ,contrib.Parent.ID,"contrib.name",contrib.ID),e);
289+ if(ReportError(string.Format(templ,contrib.Parent.ID,"contrib.name",contrib.ID),e))
290+ throw;
285291 }
286292 }
287293 foreach (Contribution contrib in handlers.Keys) {
@@ -294,7 +300,8 @@
294300 } catch (Exception e) {
295301 contrib._state = InstallationState.FatalError;
296302 string templ = Main.resources["plugin.contrib_init_error"].stringValue;
297- ReportError(string.Format(templ, contrib.Parent.ID, "contrib.name", contrib.ID), e);
303+ if(ReportError(string.Format(templ, contrib.Parent.ID, "contrib.name", contrib.ID), e))
304+ throw;
298305 }
299306 }
300307 }
@@ -309,18 +316,24 @@
309316 }
310317 }
311318
312- internal void ReportError(string msg, Exception e)
319+ /// <summary>
320+ /// Show Plugin Error Form. Should throw again when true returns.
321+ /// </summary>
322+ /// <param name="msg"></param>
323+ /// <param name="e"></param>
324+ /// <returns></returns>
325+ internal bool ReportError(string msg, Exception e)
313326 {
314327 errCount++;
315328 if (errCount < SkipTooMenyErrorsCount) {
316- UIUtil.ShowException(msg, e, UIInformLevel.minor);
317- return;
329+ return UIUtil.ShowException(msg, e, UIInformLevel.minor);
318330 } else {
319331 if (errCount == SkipTooMenyErrorsCount) {
320332 string templ = Main.resources["plugin.too_many_errors"].stringValue;
321- UIUtil.ShowException(templ, new Exception(templ,e), UIInformLevel.minor);
333+ Exception ex = new Exception(templ,e);
334+ UIUtil.ShowException(templ, ex, UIInformLevel.minor);
322335 }
323- return; // skip too many
336+ return false; // skip too many
324337 }
325338 }
326339 #endregion
@@ -440,7 +453,8 @@
440453 string msg = MakeContribExceptionMessage(p, ctb_reader);
441454 if (p._state != InstallationState.FatalError)
442455 p._state = InstallationState.PartialError;
443- ReportError(msg, e);
456+ if(ReportError(msg, e))
457+ throw;
444458 }
445459 }
446460 if (p._state != InstallationState.FatalError && errors == count) {
--- framework/framework/plugin/PluginXmlException.cs (revision 6)
+++ framework/framework/plugin/PluginXmlException.cs (revision 7)
@@ -23,41 +23,43 @@
2323 :this(node,msg)
2424 {
2525 this.plugin = p;
26- ConfirmPlugin(node);
2726 }
2827
2928 public PluginXmlException(Contribution c, ParamsReader node, string msg)
3029 :this(node,msg)
3130 {
32- this.contrib = c;
33- this.plugin = c.Parent;
34- ConfirmPlugin(node);
31+ AttachContribution(c);
3532 }
3633
37- private void ConfirmPlugin(ParamsReader node)
34+ public void AttachContribution(Contribution c){
35+ this.contrib = c;
36+ this.plugin = c.Parent;
37+ }
38+
39+ private bool ConfirmPlugin()
3840 {
39- if(plugin==null)
40- {
41- string pname = Path.GetFileName(Path.GetDirectoryName(node.SourceURI)+"");
42- plugin = PluginManager.theInstance.GetPlugin(pname);
43- }
41+ if (plugin != null) return true;
42+
43+ string pname = Path.GetFileName(Path.GetDirectoryName(node.SourceURI)+"");
44+ plugin = PluginManager.theInstance.GetPlugin(pname);
45+ return plugin != null;
4446 }
4547
4648 public string getDetailedMessage(string linecode)
4749 {
4850 string msg = base.Message;
49- if(plugin==null)
51+ if (!ConfirmPlugin())
5052 {
51- msg+=string.Format("Plug-In:{0}","N/A");
53+ msg+=string.Format(" Plug-In:{0}","N/A");
5254 return msg;
5355 }
54- msg+=string.Format("Plug-In:{0}{1}",plugin.ID,linecode);
55- msg+=string.Format("Title:{0}{1}",plugin.Title,linecode);
56- msg+=string.Format("Author:{0}",plugin.author);
56+ msg+=string.Format(" Plug-In:{0}{1}",plugin.ID,linecode);
57+ msg+=string.Format(" Title:{0}{1}",plugin.Title,linecode);
58+ msg+=string.Format(" Author:{0}",plugin.author);
5759 if(contrib!=null)
5860 {
59- msg+=string.Format("{0}Contrib-ID:{1}",linecode,contrib.ID);
60- msg+=string.Format("{0}Name:{1}",linecode,contrib.Name);
61+ msg+=string.Format(" {0}Contrib-ID:{1}",linecode,contrib.ID);
62+ msg+=string.Format(" {0}Name:{1}",linecode,contrib.Name);
6163 }
6264 return msg;
6365 }
--- framework/framework/WebLoader.cs (revision 6)
+++ framework/framework/WebLoader.cs (revision 7)
@@ -50,7 +50,8 @@
5050 {
5151 Debug.WriteLine(e);
5252 string templ = Main.resources["webloader.other_exception"].stringValue;
53- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
53+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
54+ throw;
5455 }
5556 return null;
5657 }
@@ -77,7 +78,8 @@
7778 {
7879 Debug.WriteLine(e);
7980 string templ = Main.resources["webloader.other_exception"].stringValue;
80- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
81+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
82+ throw;
8183 }
8284
8385 }
@@ -103,7 +105,8 @@
103105 {
104106 Debug.WriteLine(e);
105107 string templ = Main.resources["webloader.other_exception"].stringValue;
106- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
108+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
109+ throw;
107110 }
108111 finally
109112 {
@@ -140,7 +143,8 @@
140143 {
141144 Debug.WriteLine(e);
142145 string templ = Main.resources["webloader.other_exception"].stringValue;
143- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
146+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
147+ throw;
144148 }
145149 }
146150
@@ -183,7 +187,8 @@
183187 {
184188 Debug.WriteLine(e);
185189 string templ = Main.resources["webloader.other_exception"].stringValue;
186- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
190+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
191+ throw;
187192 }
188193 }
189194
@@ -231,7 +236,8 @@
231236 Debug.WriteLine(e);
232237 Release();
233238 string templ = Main.resources["webloader.other_exception"].stringValue;
234- UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal);
239+ if(UIUtil.ShowException(string.Format(templ,url),e,UIInformLevel.normal))
240+ throw;
235241 }
236242 }
237243
--- framework/ui/InformationService.cs (revision 6)
+++ framework/ui/InformationService.cs (revision 7)
@@ -43,9 +43,9 @@
4343 }
4444 }
4545
46- public void ShowException(string text,Exception e,UIInformLevel level)
46+ public bool ShowException(string text,Exception e,UIInformLevel level)
4747 {
48- UIUtil.ShowErrorMessageBox(null, text, e);
48+ return UIUtil.ShowErrorMessageBox(null, text, e);
4949 }
5050 public void ShowMessage(string text,UIMessageType type,UIInformLevel level)
5151 {
--- framework/ui/command/CommandUI2.cs (revision 6)
+++ framework/ui/command/CommandUI2.cs (revision 7)
@@ -114,7 +114,8 @@
114114 {
115115 string templ = Main.resources["command.exception@execute"].stringValue;
116116 string msg = string.Format(templ,new string[]{ID});
117- UIUtil.ShowException(msg,e,UIInformLevel.normal);
117+ if(UIUtil.ShowException(msg,e,UIInformLevel.normal))
118+ throw;
118119 }
119120 }
120121 }
--- framework/util/UIUtil.cs (revision 6)
+++ framework/util/UIUtil.cs (revision 7)
@@ -85,9 +85,18 @@
8585 {
8686 return handler.ShowQueryMessage(text,UIQueryType.ok_cancel,UIMessageType.question,UIInformLevel.normal);
8787 }
88- static public void ShowException(string text,Exception caused,UIInformLevel level)
88+
89+ /// <summary>
90+ /// Show Exception Detail Form. Should throw again when true returns.
91+ /// ex. if(UIUtil.ShowException(text,caused,level)) throw;
92+ /// </summary>
93+ /// <param name="text"></param>
94+ /// <param name="caused"></param>
95+ /// <param name="level"></param>
96+ /// <returns>Should throw again if true returns</returns>
97+ static public bool ShowException(string text,Exception caused,UIInformLevel level)
8998 {
90- handler.ShowException(text,caused,level);
99+ return handler.ShowException(text,caused,level);
91100 }
92101
93102 static public void SetHandler(IMessageUIHandler new_handler)
@@ -106,11 +115,11 @@
106115 {
107116 Debug.WriteLine(text);
108117 }
109- public void ShowException(string text,Exception caused,UIInformLevel level)
118+ public bool ShowException(string text,Exception caused,UIInformLevel level)
110119 {
111- ShowErrorMessageBox(null,text,caused);
112- Debug.WriteLine(text);
113- Debug.WriteLine(caused.StackTrace);
120+ Debug.WriteLine(text);
121+ Debug.WriteLine(caused.StackTrace);
122+ return ShowErrorMessageBox(null, text, caused);
114123 }
115124 public bool ShowQueryMessage(string text,UIQueryType query,UIMessageType type,UIInformLevel level)
116125 {
@@ -121,25 +130,27 @@
121130 }
122131 }
123132
124- public static void ShowErrorMessageBox(IWin32Window owner, string msg, Exception e )
133+ public static bool ShowErrorMessageBox(IWin32Window owner, string msg, Exception e )
125134 {
126135 Assembly asm = Assembly.LoadFrom(Directories.AppBaseDir+"NFT.UI.DLL");
127136 Type t = asm.GetType("nft.ui.system.ErrorMessageBox");
128137 BindingFlags flags = BindingFlags.Public|BindingFlags.Static|BindingFlags.InvokeMethod;
129- object ret = t.InvokeMember("Show",flags ,null,null,new object[]{owner,msg,e});
130- if ((bool)ret && Main.mainFrame!=null) {
131- DialogResult r2 = MessageBox.Show(null, "Are you sure to QUIT?", "NeoFT", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
138+ DialogResult ret = (DialogResult)t.InvokeMember("Show", flags, null, null, new object[] { owner, msg, e });
139+ if (DialogResult.Abort==ret && Main.mainFrame!=null) {
140+ string msg2 = Main.resources["ui_util.confirm_exit_message"].stringValue;
141+ DialogResult r2 = MessageBox.Show(null, msg2, "NeoFT", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
132142 if (r2 == DialogResult.Yes) {
133- Environment.Exit(-2);
143+ Environment.Exit(0);
134144 }
135145 }
146+ return DialogResult.Retry == ret;
136147 }
137148 }
138149
139150 public interface IMessageUIHandler
140151 {
141- void ShowMessage(string text,UIMessageType type,UIInformLevel level);
142- void ShowException(string text,Exception caused,UIInformLevel level);
152+ void ShowMessage(string text,UIMessageType type,UIInformLevel level);
153+ bool ShowException(string text,Exception caused,UIInformLevel level);
143154 bool ShowQueryMessage(string text,UIQueryType query,UIMessageType type,UIInformLevel level);
144155 void Release();
145156 }