Index: oup/rewrite/Tools/BinEdit.pas
===================================================================
--- oup/rewrite/Tools/BinEdit.pas	(revision 94)
+++ oup/rewrite/Tools/BinEdit.pas	(revision 97)
@@ -140,5 +140,5 @@
 var
   mem:  TMemoryStream;
-  Data: Tdata;
+  Data: TByteData;
 begin
   if con <> nil then
@@ -397,5 +397,5 @@
 procedure TForm_BinEdit.WriteValues;
 var
-  i, j:  Byte;
+  i, j:  Integer;
   Data:  Tdata;
   str:   String;
Index: oup/rewrite/Tools/Preview.pas
===================================================================
--- oup/rewrite/Tools/Preview.pas	(revision 94)
+++ oup/rewrite/Tools/Preview.pas	(revision 97)
@@ -4,5 +4,5 @@
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, StdCtrls, Template, ExtCtrls, Math, StrUtils,
-  ConnectionManager, {OniImgClass,} Data, TypeDefs, Menus, Buttons;
+  ConnectionManager, OniImgClass, Data, TypeDefs, Menus, Buttons;
 
 type
@@ -84,5 +84,5 @@
 procedure TForm_Preview.LoadImage(fileid, index: Integer);
 var
-  Data:      TByteArray;
+  Data:      TByteData;
   memstream: TMemoryStream;
   OniImage:  TOniImage;
@@ -90,6 +90,6 @@
 begin
   OniImage := TOniImage.Create;
-  OniImage.Load(Connection, fileid);
-  Data := OniImage.GetAsBMP;
+  OniImage.Load(ConnectionID, fileid);
+  OniImage.GetAsBMP(Data);
   OniImage.Free;
 
@@ -147,10 +147,10 @@
   i:    Byte;
 begin
-  Connection.LoadDatFilePart(_fileid, $14, SizeOf(loop_speed), @loop_speed);
-  Connection.LoadDatFilePart(_fileid, $1C, SizeOf(linkcount), @linkcount);
+  ConManager.Connection[ConnectionID].LoadDatFilePart(_fileid, $14, SizeOf(loop_speed), @loop_speed);
+  ConManager.Connection[ConnectionID].LoadDatFilePart(_fileid, $1C, SizeOf(linkcount), @linkcount);
   SetBitmapCount(linkcount);
   for i := 0 to linkcount - 1 do
   begin
-    Connection.LoadDatFilePart(_fileid, $20 + i * 4, SizeOf(link), @link);
+    ConManager.Connection[ConnectionID].LoadDatFilePart(_fileid, $20 + i * 4, SizeOf(link), @link);
     link := link div 256;
     if link = 0 then
@@ -190,5 +190,5 @@
   else
     actualimg := High(bitmaps);
-  Self.Caption := 'Preview ' + Connection.GetFileInfo(_fileid).FileName +
+  Self.Caption := 'Preview ' + ConManager.Connection[ConnectionID].GetFileInfo(_fileid).Name +
     ' (' + IntToStr(actualimg + 1) + '/' + IntToStr(Length(bitmaps)) + ')';
   DrawImage(actualimg);
@@ -202,5 +202,5 @@
   else
     actualimg := 0;
-  Self.Caption := 'Preview ' + Connection.GetFileInfo(_fileid).FileName +
+  Self.Caption := 'Preview ' + ConManager.Connection[ConnectionID].GetFileInfo(_fileid).Name +
     ' (' + IntToStr(actualimg + 1) + '/' + IntToStr(Length(bitmaps)) + ')';
   DrawImage(actualimg);
Index: oup/rewrite/Tools/RawEdit.pas
===================================================================
--- oup/rewrite/Tools/RawEdit.pas	(revision 94)
+++ oup/rewrite/Tools/RawEdit.pas	(revision 97)
@@ -4,6 +4,6 @@
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, Template, StdCtrls, ExtCtrls, Menus, Grids, Wrapgrid,
-  MPHexEditor, Clipbrd, StrUtils,
-  Data, Functions, DataStructures, Exporters, OniDataClass, Buttons;
+  MPHexEditor, Clipbrd, StrUtils, TypeDefs,
+  Data, Functions, DatStructureLoader, ConnectionManager, Buttons;
 
 type
@@ -29,5 +29,5 @@
     procedure list_offsetClick(Sender: TObject);
     procedure NewFile(fileinfo: TFileInfo);
-    procedure LoadRaw(raw_info: TRawInfo);
+    procedure LoadRaw(raw_info: TRawDataInfo);
     function Save: Boolean;
 
@@ -259,5 +259,5 @@
 procedure TForm_RawEdit.WriteValues;
 var
-  i, j:  Byte;
+  i, j:  Integer;
   Data:  Tdata;
   str:   String;
Index: oup/rewrite/Tools/Template.dfm
===================================================================
--- oup/rewrite/Tools/Template.dfm	(revision 94)
+++ oup/rewrite/Tools/Template.dfm	(revision 97)
@@ -19,5 +19,4 @@
   OnActivate = FormActivate
   OnClose = FormClose
-  OnCreate = FormCreate
   OnResize = FormResize
   PixelsPerInch = 96
Index: oup/rewrite/Tools/Template.pas
===================================================================
--- oup/rewrite/Tools/Template.pas	(revision 94)
+++ oup/rewrite/Tools/Template.pas	(revision 97)
@@ -6,9 +6,10 @@
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, ExtCtrls, StdCtrls, StrUtils,
-  ConnectionManager, {Functions,} Data, TypeDefs, Menus, Buttons;
+  Data, TypeDefs, Menus, Buttons;
 
 type
   TNewFileSelectedEvent = procedure(FileInfo: TFileInfo) of object;
   TNewConnectionEvent = procedure(Connection: Integer) of object;
+  TCheckCloseableEvent = function: Boolean of object;
 
   TForm_ToolTemplate = class(TForm)
@@ -40,10 +41,10 @@
     combo_connection: TComboBox;
     Bevel1: TBevel;
-    procedure RecreateList;
-    procedure UpdateList;
     procedure RecreateExtList;
+    procedure UpdateConList;
     procedure LoadFileNames;
-    procedure SelectFileName(connection: Integer; FileName: String);
-    procedure SelectFileID(connection: Integer; FileID: Integer);
+    procedure SelectFileName(ConnectionID: Integer; FileName: String);
+    procedure SelectFileID(ConnectionID, FileID: Integer);
+    procedure SelectConnection(ConnectionID: Integer);
     procedure check_filternameClick(Sender: TObject);
     procedure check_zerobyteClick(Sender: TObject);
@@ -54,5 +55,4 @@
 
     procedure FormResize(Sender: TObject);
-    procedure FormCreate(Sender: TObject);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
     procedure popup_importClick(Sender: TObject);
@@ -67,10 +67,12 @@
     FOnNewFileSelected: TNewFileSelectedEvent;
     FOnNewConnection: TNewConnectionEvent;
+    FOnCheckCloseable: TCheckCloseableEvent;
     FAllowedExts: String;
     FAllowMultiSelect: Boolean;
     FSelectedFile: TFileInfo;
-    FConnection: Integer;
+    FConnectionID: Integer;
     procedure SetAllowedExts(exts: String);
     procedure SetMultiSelect(allow: Boolean);
+    function GetToolCloseable: Boolean;
   public
     constructor Create(AOwner: TComponent); override;
@@ -79,8 +81,10 @@
     property OnNewFileSelected: TNewFileSelectedEvent read FOnNewFileSelected write FOnNewFileSelected;
     property OnNewConnection: TNewConnectionEvent read FOnNewConnection write FOnNewConnection;
+    property OnCheckCloseable: TCheckCloseableEvent read FOnCheckCloseable write FOnCheckCloseable;
     property AllowedExts: String read FAllowedExts write SetAllowedExts;
     property AllowMultiSelect: Boolean read FAllowMultiSelect write SetMultiSelect;
     property SelectedFile: TFileInfo read FSelectedFile;
-    property Connection: Integer read FConnection;
+    property ConnectionID: Integer read FConnectionID;
+    property Closeable: Boolean read GetToolCloseable;
   end;
 
@@ -91,26 +95,24 @@
 implementation
 {$R *.dfm}
-uses Main, Exporters;
-
-
-procedure TForm_ToolTemplate.UpdateList;
+uses Main, ConnectionManager, Exporters, Functions;
+
+
+procedure TForm_ToolTemplate.UpdateConList;
 var
   i: Integer;
   fn, datatype, boxstring: String;
   level: Integer;
-  oldcon: String;
-begin
-  oldcon := combo_connection.Items.Strings[combo_connection.ItemIndex];
+begin
   combo_connection.ItemIndex := -1;
   combo_connection.Items.Clear;
-  if Length(DataConnections) > 0 then
-  begin
-    for i := 0 to High(DataConnections) do
+  if ConManager.Count > 0 then
+  begin
+    for i := 0 to ConManager.Count - 1 do
     begin
-      level := DataConnections[i].LevelInfo.LevelNumber;
-      fn := ExtractFileName(DataConnections[i].FileName);
-      if DataConnections[i].Backend = ODB_Dat then
+      level := ConManager.ConnectionByIndex[i].LevelNumber;
+      fn := ExtractFileName(ConManager.ConnectionByIndex[i].FileName);
+      if ConManager.ConnectionByIndex[i].Backend = DB_ONI then
         datatype := 'ONI-.dat: '
-      else if DataConnections[i].Backend = ODB_ADB then
+      else if ConManager.ConnectionByIndex[i].Backend = DB_ADB then
         datatype := 'OUP-DB: '
       else
@@ -118,5 +120,5 @@
       boxstring := datatype + fn + ' (Level: ' + IntToStr(level) + ')';
       combo_connection.Items.Add(boxstring);
-      if oldcon = boxstring then
+      if ConManager.ConnectionByIndex[i].ConnectionID = FConnectionID then
         combo_connection.ItemIndex := combo_connection.Items.Count - 1;
     end;
@@ -129,9 +131,9 @@
   else
   begin
-    FConnection := nil;
+    FConnectionID := 0;
     filelist.Items.Clear;
     combo_extension.Items.Clear;
     combo_connectionChange(Self);
-    FSelectedFile := GetEmptyFileInfo;
+    FSelectedFile.ID := -1;
     if Assigned(FOnNewFileSelected) then
       FOnNewFileSelected(FSelectedFile);
@@ -139,60 +141,26 @@
 end;
 
-procedure TForm_ToolTemplate.RecreateList;
-var
-  i: Integer;
-  fn, datatype: String;
-  level: Integer;
-begin
-  combo_connection.Items.Clear;
-  if Length(DataConnections) > 0 then
-  begin
-    for i := 0 to High(DataConnections) do
-    begin
-      level := DataConnections[i].LevelInfo.LevelNumber;
-      fn := ExtractFileName(DataConnections[i].FileName);
-      if DataConnections[i].Backend = ODB_Dat then
-        datatype := 'ONI-.dat: '
-      else if DataConnections[i].Backend = ODB_ADB then
-        datatype := 'OUP-DB: '
-      else
-        datatype := 'Unknown: ';
-      combo_connection.Items.Add(datatype + fn + ' (Level: ' + IntToStr(level) + ')');
-    end;
-    FConnection := DataConnections[0];
-    combo_connection.ItemIndex := 0;
-    combo_connectionChange(Self);
-  end
-  else
-  begin
-    FConnection := nil;
-    filelist.Items.Clear;
-    combo_extension.Items.Clear;
-    combo_connectionChange(Self);
-  end;
-end;
-
 procedure TForm_ToolTemplate.RecreateExtList;
 var
-  i:    LongWord;
-  exts: TStringArray;
+  i:    Integer;
+  exts: TStrings;
 begin
   combo_extension.Items.Clear;
-  if FConnection <> nil then
+  if FConnectionID > -1 then
   begin
     combo_extension.Items.Add('_All files_ (' +
-      IntToStr(FConnection.GetFilesCount) + ')');
-    exts := FConnection.GetExtensionsList;
-    for i := 0 to High(exts) do
+      IntToStr(ConManager.Connection[FConnectionID].GetFileCount) + ')');
+    exts := ConManager.Connection[FConnectionID].GetExtensionsList(EF_ExtCount);
+    for i := 0 to exts.Count - 1 do
       if Length(FAllowedExts) > 0 then
       begin
-        if Pos(MidStr(exts[i],1,4), FAllowedExts) > 0 then
-        begin
-          combo_extension.Items.Add(exts[i]);
-        end;
-      end else
-        combo_extension.Items.Add(exts[i]);
+        if Pos(MidStr(exts.Strings[i],1,4), FAllowedExts) > 0 then
+          combo_extension.Items.Add(exts.Strings[i]);
+      end
+      else
+        combo_extension.Items.Add(exts.Strings[i]);
     combo_extension.ItemIndex := 0;
     combo_extensionClick(Self);
+    exts.Free;
   end;
 end;
@@ -206,8 +174,8 @@
   no_zero_bytes: Boolean;
   pattern: String;
-  files: TStringArray;
-  i: LongWord;
-begin
-  if FConnection <> nil then
+  files: TStrings;
+  i: Integer;
+begin
+  if FConnectionID > -1 then
   begin
     Extension := MidStr(combo_extension.Items.Strings[combo_extension.ItemIndex], 1, 4);
@@ -222,11 +190,10 @@
         Extension := '';
 
-    files := FConnection.GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
+    files := ConManager.Connection[FConnectionID].GetFilesList(extension, pattern, no_zero_bytes, FSortBy);
 
     filelist.Visible := False;
     filelist.Items.Clear;
-    if Length(files) > 0 then
-      for i := 0 to High(files) do
-        filelist.Items.Add(files[i]);
+    if files.Count > 0 then
+      filelist.Items.AddStrings(files);
     filelist.Visible := True;
   end;
@@ -239,10 +206,10 @@
   ext: String;
 begin
-  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
+  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
   ext := RightStr(filelist.Items.Strings[filelist.ItemIndex], 4);
   exportd.Filter := 'Files of matching extension (*.' + ext + ')|*.' + ext + '|All files|*.*';
   exportd.DefaultExt := ext;
   if exportd.Execute then
-    ExportDatFile(FConnection, id, exportd.FileName);
+    ExportDatFile(FConnectionID, id, exportd.FileName);
 end;
 
@@ -252,8 +219,7 @@
   finfo: TFileInfo;
   fs: TFileStream;
-  data: TData;
-begin
-  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
-  finfo := FConnection.GetFileInfo(id);
+begin
+  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
+  finfo := ConManager.Connection[FConnectionID].GetFileInfo(id);
 
   importd.Filter := 'Files of matching extension (*.' + finfo.Extension + ')|*.' +
@@ -268,7 +234,5 @@
         'Size of chosen file: ' + FormatFileSize(fs.Size))
     else begin
-      SetLength(data, fs.Size);
-      fs.Read(data[0], fs.Size);
-      FConnection.UpdateDatFile(id, data);
+      ConManager.Connection[FConnectionID].UpdateDatFile(id, fs);
       Self.listClick(Self);
     end;
@@ -283,7 +247,7 @@
 begin
   sender_name := TComponent(Sender).Name;
-  id := FConnection.ExtractFileID(filelist.Items.Strings[filelist.ItemIndex]);
+  id := ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[filelist.ItemIndex]);
   context := MidStr(sender_name, Pos('_', sender_name) + 1, Length(sender_name) - Pos('_', sender_name));
-  Form_Main.open_child(context, FConnection, id);
+  Form_Main.open_child(context, FConnectionID, id);
 end;
 
@@ -295,25 +259,10 @@
 begin
   if combo_connection.ItemIndex >= 0 then
-  begin
-    name := combo_connection.Items.Strings[combo_connection.ItemIndex];
-    nstart := Pos(' ', name) + 1;
-    nend := Pos('(', name) - 1;
-    name := MidStr(name, nstart, nend - nstart);
-
-    for i := 0 to High(DataConnections) do
-    begin
-      if ExtractFileName(DataConnections[i].FileName) = name then
-      begin
-        FConnection := DataConnections[i];
-        Break;
-      end;
-    end;
-    if i = Length(DataConnections) then
-      FConnection := nil;
-
-    RecreateExtList;
-    if Assigned(FOnNewConnection) then
-      FOnNewConnection(FConnection);
-  end;
+    FConnectionID := combo_connection.ItemIndex
+  else
+    FConnectionID := -1;
+  RecreateExtList;
+  if Assigned(FOnNewConnection) then
+    FOnNewConnection(FConnectionID);
 end;
 
@@ -330,6 +279,14 @@
 begin
   inherited;
-  RecreateList;
-  FSelectedFile := GetEmptyFileInfo;
+  Self.Width  := 260;
+  Self.Height := 300;
+  FAllowedExts := '';
+  FAllowMultiSelect := False;
+  FOnNewFileSelected := nil;
+  FOnNewConnection := nil;
+  FOnCheckCloseable := nil;
+  FConnectionID := -1;
+  FSelectedFile.ID := -1;
+  UpdateConList;
   if Length(ToolList) > 0 then
   begin
@@ -368,15 +325,15 @@
 begin
   if btn_sort_id_asc.Down then
-    FSortBy := stIDAsc
+    FSortBy := ST_IDAsc
   else if btn_sort_id_desc.Down then
-    FSortBy := stIDDesc
+    FSortBy := ST_IDDesc
   else if btn_sort_name_asc.Down then
-    FSortBy := stNameAsc
+    FSortBy := ST_NameAsc
   else if btn_sort_name_desc.Down then
-    FSortBy := stNameDesc
+    FSortBy := ST_NameDesc
   else if btn_sort_ext_asc.Down then
-    FSortBy := stExtAsc
+    FSortBy := ST_ExtAsc
   else if btn_sort_ext_desc.Down then
-    FSortBy := stExtDesc;
+    FSortBy := ST_ExtDesc;
   LoadFileNames;
 end;
@@ -394,7 +351,7 @@
   if filelist.ItemIndex > -1 then
   begin
-    fileid := FConnection.ExtractFileID(
+    fileid := ConManager.Connection[FConnectionID].ExtractFileIDOfName(
           filelist.Items.Strings[filelist.ItemIndex]);
-    FSelectedFile := FConnection.GetFileInfo(fileid);
+    FSelectedFile := ConManager.Connection[FConnectionID].GetFileInfo(fileid);
     if Assigned(FOnNewFileSelected) then
       FOnNewFileSelected(FSelectedFile);
@@ -415,28 +372,24 @@
 
 
-procedure TForm_ToolTemplate.SelectFileID(connection: TOniData; id: Integer);
-var
-  i: Integer;
-  name: String;
-  nstart, nend: Integer;
-begin
-  for i := 0 to combo_connection.Items.Count - 1 do
-  begin
-    name := combo_connection.Items.Strings[i];
-    nstart := Pos(' ', name) + 1;
-    nend := Pos('(', name) - 1;
-    name := MidStr(name, nstart, nend - nstart);
-
-    if ExtractFileName(connection.FileName) = name then
-    begin
-      combo_connection.ItemIndex := i;
-      combo_connectionChange(Self);
-    end;
-  end;
+procedure TForm_ToolTemplate.SelectConnection(ConnectionID: Integer);
+begin
+  if FConnectionID <> ConnectionID then
+  begin
+    combo_connection.ItemIndex := ConManager.ConnectionIndexByID[ConnectionID];
+    combo_connectionChange(Self);
+  end;
+end;
+
+procedure TForm_ToolTemplate.SelectFileID(ConnectionID, FileID: Integer);
+var
+  i: Integer;
+begin
+  if FConnectionID <> ConnectionID then
+    SelectConnection(ConnectionID);
 
   filelist.ItemIndex := -1;
   if filelist.Items.Count > 0 then
     for i := 0 to filelist.Items.Count - 1 do
-      if FConnection.ExtractFileID(filelist.Items.Strings[i]) = id then
+      if ConManager.Connection[FConnectionID].ExtractFileIDOfName(filelist.Items.Strings[i]) = FileID then
       begin
         filelist.ItemIndex := i;
@@ -446,23 +399,10 @@
 end;
 
-procedure TForm_ToolTemplate.SelectFileName(connection: TOniData; filename: String);
-var
-  i: Integer;
-  name: String;
-  nstart, nend: Integer;
-begin
-  for i := 0 to combo_connection.Items.Count - 1 do
-  begin
-    name := combo_connection.Items.Strings[i];
-    nstart := Pos(' ', name) + 1;
-    nend := Pos('(', name) - 1;
-    name := MidStr(name, nstart, nend - nstart);
-
-    if ExtractFileName(connection.FileName) = name then
-    begin
-      combo_connection.ItemIndex := i;
-      combo_connectionChange(Self);
-    end;
-  end;
+procedure TForm_ToolTemplate.SelectFileName(ConnectionID: Integer; filename: String);
+var
+  i: Integer;
+begin
+  if FConnectionID <> ConnectionID then
+    SelectConnection(ConnectionID);
 
   filelist.ItemIndex := -1;
@@ -519,12 +459,10 @@
 
 
-procedure TForm_ToolTemplate.FormCreate(Sender: TObject);
-begin
-  Self.Width  := 260;
-  Self.Height := 300;
-  FOnNewFileSelected := nil;
-  FOnNewConnection := nil;
-  FAllowedExts := '';
-  FAllowMultiSelect := False;
+function TForm_ToolTemplate.GetToolCloseable: Boolean;
+begin
+  if Assigned(FOnCheckCloseable) then
+    Result := FOnCheckCloseable
+  else
+    Result := True;
 end;
 
Index: oup/rewrite/Tools/TxmpReplace.pas
===================================================================
--- oup/rewrite/Tools/TxmpReplace.pas	(revision 94)
+++ oup/rewrite/Tools/TxmpReplace.pas	(revision 97)
@@ -4,5 +4,5 @@
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, Template, StdCtrls, ExtCtrls,
-  Functions, Data, OniImgClass, Menus, Buttons;
+  Functions, Data, OniImgClass, Menus, Buttons, TypeDefs;
 
 type
@@ -41,5 +41,5 @@
 implementation
 {$R *.dfm}
-uses Main, OniDataClass;
+uses Main, ConnectionManager;
 
 
@@ -47,5 +47,5 @@
 procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
 var
-  Data: Tdata;
+  Data: TByteData;
   mem:  TMemoryStream;
   fadingbyte, depthbyte, storebyte: Byte;
