原文转自:http://blog.csdn.net/jk276993857/article/details/5732686
* 一、主函数
* WW程序的入口在WorldWind.cs的Main函数中,其中,args可通过命令行等方式进行
* 传递参数,例如程序目录等。详见ParseArgs(args)。在主函数中,包括以下部分:
* 1. 获得该应用程序的版本号
* 2. 如果已经运行了WW,将该实例参数传出,并退出
* 3. 如果服务器上有超过50个用户同时在线则退出
* 4. 命名主线程
* 5. 解析由命令行输入的args参数
* 6. 载入配置文件 (重点***)
* 7. 添加线程异常事件处理句柄
* 8. 创建主应用程序实例 (重点*****)
* 9. 保存World设置
* 10.在保存程序设置之前对用户资格证书进行加密
* 11.保存程序设置
static void Main(string[] args)
{
// Establish the version number string used for user display,
// such as the Splash and Help->About screens.
// To change the Application.ProductVersion make the
// changes in /WorldWind/AssemblyInfo.cs
// For alpha/beta versions, include " alphaN" or " betaN"
// at the end of the format string.
// 1. 获得该应用程序的版本号,可通过AssemblyInfo.cs下进行修改
Version ver = new Version(Application.ProductVersion);
Release = string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
// If World Wind is already running, pass any commandline
// arguments from this instance, and quit.
// 2. 如果已经运行了WW,将该实例参数传出,并退出
IntPtr handle = GetWWHandle();
if (!System.IntPtr.Zero.Equals(handle))
{
if(args.Length>0)
NativeMethods.SendArgs( handle, string.Join("/n",args) );
return;
}
// abort if 50 bindings problem present and user opts to go to the download page
// 3. 如果服务器上有超过50个用户同时在线则退出,并使用户选择是否进入下载页面
if(BindingsCheck.FiftyBindingsWarning()) return;
// Name the main thread
// 4. 命名主线程
System.Threading.Thread.CurrentThread.Name = "Main Thread";
// ParseArgs may set values that are used elsewhere,
// such as startFullScreen and CurrentSettingsDirectory.
// 5. 解析命令行参数至cmdArgs,(以'/'或WorldWindUri格式开头)
// 根据命令行参数对变量赋值以便在其他地方使用,如startFullScreen and CurrentSettingsDirectory.
ParseArgs(args);
// 6. 载入配置文件,如果配置文件目录不存在则设置为默认目录
// 配置信息将保存入Settings中,并由World进行静态变量保存
// 注意此处的CurrentSettingsDirectory可能就是从解析参数中获得的
if(CurrentSettingsDirectory == null)
{
// load program settings from default directory
// 从默认的文件夹中载入设置
LoadSettings();
World.LoadSettings();
}
else
{
LoadSettings(CurrentSettingsDirectory);
World.LoadSettings(CurrentSettingsDirectory);
}
// 7. 添加线程异常事件处理句柄
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
// 8. 创建主应用程序实例,并将此应用程序添加Idle事件,主要用于控制渲染线程
MainApplication app = new MainApplication();
Application.Idle += new EventHandler(app.WorldWindow.OnApplicationIdle);
Application.Run(app);
// Save World settings
// 9. 保存World设置
World.Settings.Save();
// Encrypt encoded user credentials before saving program settings
// 10. 在保存程序设置之前对用户资格证书进行加密
DataProtector dp = new DataProtector(DataProtector.Store.USE_USER_STORE);
Settings.ProxyUsername = dp.TransparentEncrypt(Settings.ProxyUsername);
Settings.ProxyPassword = dp.TransparentEncrypt(Settings.ProxyPassword);
// Save program settings
// 11. 保存程序设置
Settings.Save();
}
* 二、LoadSettings()
* 1. 除非在args里传入CurrentSettingsDirectory参数,否则都进入默认目录下找配置文件
* 2. 默认配置文件目录 window7 和 XP 下的目录不一样
* "C:/Documents and Settings/XXXXXX/Application Data/NASA/World Wind/1.4.0.0/WorldWind.xml"
* "C:/Users/XXXXXX/AppData/Roaming/NASA/World Wind/1.4.0.0/WorldWind.xml"
* XXXXX为相应的用户,上面的是XP目录,下面的是win7目录
* 在SettingsBase.cs中有专门设置默认路径的函数
*
public static string DefaultLocation(LocationType locationType)
{
string directory;
switch(locationType)
{
case LocationType.UserLocal:
// Example: @"C:/Documents and Settings/<user>/Local Settings/Application Data/NASA/NASA World Wind/1.3.3.11250"
return Application.LocalUserAppDataPath;
case LocationType.UserCommon:
// Example: @"C:/Documents and Settings/All Users/Application Data/NASA/NASA World Wind/1.3.3.11250"
return Application.CommonAppDataPath;
case LocationType.Application:
// Example: @"C:/Program Files/NASA/World Wind/"
return Application.StartupPath;
default:
// fall through to regular (roaming) user
case LocationType.User:
// Example: @"C:/Documents and Settings/<user>/Application Data/NASA/World Wind/1.3.3"
directory = Log.DefaultSettingsDirectory();
Directory.CreateDirectory(directory);
return directory;
}
}
* 3. 不论是哪个配置文件,最终都 将调用SettingsBase.Load(defaultSettings, fileName)
* 3. 不论是哪个配置文件,最终都 将调用SettingsBase.Load(defaultSettings, fileName)
// load settings from a given file (full path and name)
// 从一个指定文件(包括全路径和文件名)中载入设置
public static SettingsBase Load(SettingsBase defaultSettings, string fileName)
{
// remember where we loaded from for a later save
// 记录最近一次保存的载入设置文件路径
defaultSettings.m_fileName = fileName;
// return the default instance if the file does not exist
// 如果文件不存在则返回默认初始化
if(!File.Exists(fileName))
{
return defaultSettings;
}
// start out with the default instance
// 根据xml反序列化读取配置文件,在默认设置基础上进行更改
SettingsBase settings = defaultSettings;
try
{
XmlSerializer ser = new XmlSerializer(defaultSettings.GetType());
using(TextReader tr = new StreamReader(fileName))
{
settings = (SettingsBase)ser.Deserialize(tr);
settings.m_fileName = fileName; // remember where we loaded from for a later save
}
}
catch(Exception ex)
{
throw new System.Exception(String.Format("Loading settings from file '{1}' to {0} failed",
defaultSettings.GetType().ToString(), fileName), ex);
}
return settings;
}
* 此处,若存在WorldWind.xml则进行xml的反序列化,如果不存在,则进行默认配置
* 上面的默认配置中只是基本设置,进一步的设置仍然在LoadSettings()函数中实现
* 此处,对于默认基本配置只是对路径等的配置,而对于启动项目的配置,则都是
* 通过PluginsLoadedOnStartup数组来实现的。同样,在WorldWind.xml中通过反序
* 列化解释出来的项目中也存在这些项,例如,打开WorldWind.xml文件里面包含:
* 此处只是先将启动项目的名称加入到配置中,如何启动将在后续说明,但应该明确的
* 是如果需要改变启动加载的项目,需要在xml文件中修改,或先删除WorldWind.xml,
* 再在LoadSettings()函数中进行修改。针对每个加载项目的意义,也将在后面说明。
*
* 4. 备注,Settings在从默认文件中载入前,在建立实例时就已经进行了基本初始化
* 其中包括对默认路径的设置和时间计量的促发,但对于加载项必须通过xml或者
* 程序进行配置,当然不加载任何项也可以启动程序,这样可以加快初始化时间。
*
* Settings的基本配置主要包括
* Proxy(代理服务器,包括用户和密码等)
* Cache(缓存设置,包括缓存路径和大小,时间控制等),
* Plugin(加载项,此处只进行初始化)
* Miscellaneous settings(其他设置,包括启动星球,配置向导等)
* File System Settings(文件系统设置,包括Config配置和Data配置的目录)
*
* 因此如果需要对这些设置进行修改,都可以修改WorldWindSettings.cs里的内容
*
* 5. 以上是在Main函数中加载设置,另外在static MainApplication()中也可先载入
*
* 三、World.LoadSettings()
* World.LoadSetting主要是对World类中的Setting进行配置,其类型为WorldSettings
* 第二大部分的Settings是WorldWindSettings类,这两个类的基类都属于SettingsBase
* 其相似和不同之处在于:
* 1、其配置文件名为World.xml,目录与WorldWind.xml一致,读取的反序列化方法一致
* 2、不同在于基本配置内容,在WorldSettings.cs文件中主要包含以下几个部分配置:
* 1) Atomosphere 大气层是否显示
* 2) UI 界面窗口显示,除toolbar之类的显示外还包括字体字号设置
* 3) Grid 格网设置,颜色等
* 4) World 世界图层,地名国界等
* 5) Camera 相机视频的参数设置,这直接关系到视角转动的问题
* 6) Time 时间
* 7) 3D 三维设置,如贴图格式设置成dds
* 8) Terrain 地形最小采样率
* 9) Measure tool 测量工具
* 10)Units 单位,米
* 11)Layers 图层,此处新建list,未加载任何图层loadedLayers
* 3、通过对World.xml进行加载后,会加载上一次退出时对以上参数的设置。
*
* 四、对Settings的保存Save
* 不论是对World.Settings还是对Settings的保存,都是调用了SettingsBase.Save()
// Save settings to XML file given specifically by name
// Note: the FileName property will stay unchanged
// 按指定名称将设置保存至XML文件中,注意:文件名可能将保持不变
* 对于以上WW设置的加载和保存我们可以学习以下几点
* 1)关于初始化有多种方式,一种是在程序中写死,一种是从配置文件中读取,而
* WW中采取了根据用户情况来选择,有可借鉴之处:当不存在配置文件时,则采用
* 默认配置,并将其保存以便下一次从配置文件中读取。
* 2)关于配置文件的读写方式有很多种,可以是自定义的文本格式,也可以是二进制
* 格式,有的需要保密的可采用二进制,但WW做为开源代码,很明智地选择了XML
* 的序列化与反序列化方式,这也必然成为一种通用格式的借鉴。
* 3)对于自己写程序的时候,往往对于需要配置的参数心中无数,在边写代码的过程
* 中边增加参数,或最终统一整理成配置文件,要想站在全局的观点来理解WW,还
* 需要待日后学习过程中不断反观这组配置文件,才不至于不识庐山真面目。
*
* 希望通过对初始化配置文件的学习,给日后的程序带来有条理的规划。
* It's a new beginning , It's a new start