WurmApi is a .NET library, realizing mission to enable other programmers, to easily write their own tools for Wurm Online game client and for the game in general.
A - Simple:
- Clone this repository.
- Build WurmApi in release mode.
- Reference all output DLL in your application.
B - Alternative:
- Clone this repository
- Add all WurmApi projects to your solution.
- Reference WurmApi projects in your projects.
If copying DLLs, include AldursLab.WurmApi.XML for intellisense docs.
using AldursLab.WurmApi;
Using all defaults:
using (IWurmApi api = WurmApiFactory.Create())
{
// use the API
}
Relative to WurmApi.dll:
IWurmApi api = WurmApiFactory.Create("WurmApiData");
With absolute path:
IWurmApi api = WurmApiFactory.Create("C:\WurmApps\WurmApi");
Data directory is used to store data caches, which speed up next API calls for similar data.
Do not use same directory for multiple concurrent instances of WurmApi.
Implement logging interface:
class WurmApiLogger : ILogger
{
public void Log(LogLevel level, string message, object source, Exception exception)
{
Console.WriteLine("{0}, {1}, {2}, {3}", level, message, source, exception);
}
}
Use in construction:
IWurmApi api = WurmApiFactory.Create("WurmApiData", new WurmApiLogger());
WurmApi will log any suspicious or error state during it's internal updates. Unless logging is set, these messages will go nowhere and debugging issues will be harder.
Implement event marshalling interface:
Example implementation for a WinForms application, executing all events on UI thread of MainForm.
class EventMarshaller : IEventMarshaller
{
public void Marshal(Action action)
{
Program.MainForm.BeginInvoke(action);
}
}
Use in construction:
IWurmApi api = WurmApiFactory.Create("WurmApiData", new WurmApiLogger(), new EventMarshaller());
WurmApi by default runs its events on ThreadPool, which may complicate event handling. By marshalling them to appropriate thread (most often, GUI thread), apps can remain single-threaded and still leverage WurmApi events.
If autodetection doesn't work for Wurm Client install directory, it can be overriden.
Implement install directory interface:
class WurmInstallDir : IWurmInstallDirectory
{
public string FullPath { get { return @"C:\games\Wurm"; } }
}
Use in construction:
IWurmApi api = WurmApiFactory.Create("WurmApiData", new WurmApiLogger(), new EventMarshaller(), new WurmInstallDir());
var command = @"say /time";
api.Autoruns.MergeCommandToAllAutoruns(command);
var character = api.Characters.Get("Batman");
var currentServer = character.GetCurrentServer();
var currentServerUptime = currentServer.TryGetCurrentTime();
var serverOneDayAgo = character.GetHistoricServerAtLogStamp(DateTime.Now.Subtract(TimeSpan.FromDays(1)));
var scanResults = api.LogsHistory.Scan(new LogSearchParameters()
{
CharacterName = new CharacterName("Batman"),
DateFrom = DateTime.Now.Subtract(TimeSpan.FromDays(5)),
DateTo = DateTime.Now,
LogType = LogType.Event,
});
var digsCount = scanResults.Count(entry => entry.Content.Contains("You dig a hole."));
int digsCount = 0;
EventHandler<LogsMonitorEventArgs> eventHandler = (sender, eventArgs) =>
{
digsCount += eventArgs.WurmLogEntries.Count(entry => entry.Content.Contains("You dig a hole."));
};
var batman = new CharacterName("Batman");
api.LogsMonitor.Subscribe(batman, LogType.Event, eventHandler);
Console.ReadKey();
Console.WriteLine("You have dug " + digsCount + " holes.");
api.LogsMonitor.Unsubscribe(batman, eventHandler);
var batmanSkillgainRateSetting = api.Configs.GetConfig("default").SkillGainRate;
WurmApi should always be Disposed, to ensure cleanup and consistency of data caches.
WurmApi is thread safe and a single instance is enough to handle entire app requirements.
Expensive methods have async and cancellable overloads.
Exact behavior and corner cases are documented on the API itself (xml docs).
- Mono builds for all major desktop platforms.
- Cross-platform Java bindings for the Mono build.
WurmApi is an opensource project. It is shared, so that other developers can not only use it, but continously improve it. Feedback is welcome, if there are features needed, they can be added. Feel free to ask any questions.