- Load default binds from settings.ini
authorunc0rr
Fri, 27 Sep 2013 00:54:18 +0400
changeset 9466 8b48c27201af
parent 9464 901e363d5837
child 9468 3d07fd84c39d
- Load default binds from settings.ini - Frontend doesn't send dbind commands - Refactor '/dbind' command implementation copypasta
QTfrontend/game.cpp
QTfrontend/game.h
QTfrontend/team.cpp
QTfrontend/team.h
hedgewars/hwengine.pas
hedgewars/uInputHandler.pas
hedgewars/uPhysFSLayer.pas
hedgewars/uTeams.pas
project_files/hedgewars.pro
--- a/QTfrontend/game.cpp	Sat Sep 21 23:54:57 2013 +0400
+++ b/QTfrontend/game.cpp	Fri Sep 27 00:54:18 2013 +0400
@@ -89,18 +89,6 @@
     SetGameState(gsStopped);
 }
 
-void HWGame::addKeyBindings(QByteArray * buf)
-{
-    for(int i = 0; i < BINDS_NUMBER; i++)
-    {
-        QString value = config->value(QString("Binds/%1").arg(cbinds[i].action), cbinds[i].strbind).toString();
-        if (value.isEmpty() || value == "default") continue;
-
-        QString bind = QString("edbind " + value + " " + cbinds[i].action);
-        HWProto::addStringToBuffer(*buf, bind);
-    }
-}
-
 void HWGame::commonConfig()
 {
     QByteArray buf;
@@ -118,8 +106,6 @@
     }
     HWProto::addStringToBuffer(buf, gt);
 
-    addKeyBindings(&buf);
-
     buf += gamecfg->getFullConfig();
 
     if (m_pTeamSelWidget)
@@ -132,7 +118,7 @@
             HWProto::addStringToBuffer(buf, QString("eammreinf %1").arg(ammostr.mid(3 * cAmmoNumber, cAmmoNumber)));
             if(gamecfg->schemeData(15).toBool() || !gamecfg->schemeData(21).toBool()) HWProto::addStringToBuffer(buf, QString("eammstore"));
             HWProto::addStringListToBuffer(buf,
-                                           team.teamGameConfig(gamecfg->getInitHealth(), config));
+                                           team.teamGameConfig(gamecfg->getInitHealth()));
             ;
         }
     }
@@ -150,8 +136,6 @@
     QByteArray teamscfg;
     ThemeModel * themeModel = DataManager::instance().themeModel();
 
-    addKeyBindings(&teamscfg);
-
     HWProto::addStringToBuffer(teamscfg, "TL");
     HWProto::addStringToBuffer(teamscfg, QString("etheme %1")
                                .arg((themeModel->rowCount() > 0) ? themeModel->index(rand() % themeModel->rowCount()).data(ThemeModel::ActualNameRole).toString() : "steel"));
@@ -165,7 +149,7 @@
     team1.setNumHedgehogs(4);
     HWNamegen::teamRandomNames(team1,true);
     HWProto::addStringListToBuffer(teamscfg,
-                                   team1.teamGameConfig(100, config));
+                                   team1.teamGameConfig(100));
 
     HWTeam team2;
     team2.setDifficulty(4);
@@ -175,7 +159,7 @@
         HWNamegen::teamRandomNames(team2,true);
     while(!team2.name().compare(team1.name()) || !team2.hedgehog(0).Hat.compare(team1.hedgehog(0).Hat));
     HWProto::addStringListToBuffer(teamscfg,
-                                   team2.teamGameConfig(100, config));
+                                   team2.teamGameConfig(100));
 
     HWProto::addStringToBuffer(teamscfg, QString("eammloadt %1").arg(cDefaultAmmoStore->mid(0, cAmmoNumber)));
     HWProto::addStringToBuffer(teamscfg, QString("eammprob %1").arg(cDefaultAmmoStore->mid(cAmmoNumber, cAmmoNumber)));
@@ -194,8 +178,6 @@
     HWProto::addStringToBuffer(traincfg, "eseed " + QUuid::createUuid().toString());
     HWProto::addStringToBuffer(traincfg, "escript " + training);
 
-    addKeyBindings(&traincfg);
-
     RawSendIPC(traincfg);
 }
 
@@ -207,8 +189,6 @@
 
     HWProto::addStringToBuffer(campaigncfg, "escript " + campaignScript);
 
-    addKeyBindings(&campaigncfg);
-
     RawSendIPC(campaigncfg);
 }
 
--- a/QTfrontend/game.h	Sat Sep 21 23:54:57 2013 +0400
+++ b/QTfrontend/game.h	Fri Sep 27 00:54:18 2013 +0400
@@ -113,7 +113,6 @@
         GameType gameType;
         QByteArray m_netSendBuffer;
 
-        void addKeyBindings(QByteArray * buf);
         void commonConfig();
         void SendConfig();
         void SendQuickConfig();
--- a/QTfrontend/team.cpp	Sat Sep 21 23:54:57 2013 +0400
+++ b/QTfrontend/team.cpp	Fri Sep 27 00:54:18 2013 +0400
@@ -261,7 +261,7 @@
     return true;
 }
 
-QStringList HWTeam::teamGameConfig(quint32 InitHealth, GameUIConfig * config) const
+QStringList HWTeam::teamGameConfig(quint32 InitHealth) const
 {
     QStringList sl;
     if (m_isNetTeam)
--- a/QTfrontend/team.h	Sat Sep 21 23:54:57 2013 +0400
+++ b/QTfrontend/team.h	Fri Sep 27 00:54:18 2013 +0400
@@ -93,7 +93,7 @@
         void incWins();
 
         // convert team info into strings for further computation
-        QStringList teamGameConfig(quint32 InitHealth, GameUIConfig * config) const;
+        QStringList teamGameConfig(quint32 InitHealth) const;
 
         // comparison operators
         bool operator == (const HWTeam& t1) const;
--- a/hedgewars/hwengine.pas	Sat Sep 21 23:54:57 2013 +0400
+++ b/hedgewars/hwengine.pas	Fri Sep 27 00:54:18 2013 +0400
@@ -453,7 +453,7 @@
         uChat.initModule;
         uCollisions.initModule;
         uGears.initModule;
-        uInputHandler.initModule;   //stub
+        uInputHandler.initModule;
         uMisc.initModule;
         uLandTexture.initModule;    //stub
         uScript.initModule;
--- a/hedgewars/uInputHandler.pas	Sat Sep 21 23:54:57 2013 +0400
+++ b/hedgewars/uInputHandler.pas	Fri Sep 27 00:54:18 2013 +0400
@@ -40,6 +40,8 @@
 procedure SetBinds(var binds: TBinds);
 procedure SetDefaultBinds;
 procedure chDefaultBind(var id: shortstring);
+procedure loadBinds(cmd, s: shortstring);
+procedure addBind(var binds: TBinds; var id: shortstring);
 
 procedure ControllerInit;
 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
@@ -47,7 +49,7 @@
 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
 
 implementation
-uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
+uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug, uPhysFSLayer;
 
 const
     LSHIFT = $0200;
@@ -70,7 +72,6 @@
     //ControllerBalls: array[0..5] of array[0..19] of array[0..1] of Integer;
     //ControllerHats: array[0..5] of array[0..19] of Byte;
     //ControllerButtons: array[0..5] of array[0..19] of Byte;
-    usingDBinds: boolean;
 
 function  KeyNameToCode(name: shortstring): LongInt; inline;
 begin
@@ -314,6 +315,8 @@
 DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
+
+loadBinds('dbind', cPathz[ptData] + '/settings.ini');
 end;
 
 procedure SetBinds(var binds: TBinds);
@@ -447,22 +450,68 @@
     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
 end;
 
-// Bind that isn't a team bind, but overrides defaultbinds.
-// When first called, DefaultBinds is cleared, because we assume we are getting a full list of dbinds.
-procedure chDefaultBind(var id: shortstring);
+procedure loadBinds(cmd, s: shortstring);
+var i: LongInt;
+    f: PFSFile;
+    p, l: shortstring;
+    b: byte;
+begin
+    AddFileLog('[BINDS] Loading binds from: ' + s);
+
+    l:= '';
+    if pfsExists(s) then
+        begin
+        f:= pfsOpenRead(s);
+        while (not pfsEOF(f)) and (l <> '[Binds]') do
+            pfsReadLn(f, l);
+
+        while (not pfsEOF(f)) and (l <> '') do
+            begin
+            pfsReadLn(f, l);
+
+            p:= '';
+            i:= 1;
+            while (i <= length(l)) and (l[i] <> '=') do
+                begin
+                if l[i] <> '%' then
+                    begin
+                    p:= p + l[i];
+                    inc(i)
+                    end else
+                    begin
+                    l[i]:= '$';
+                    val(copy(l, i, 3), b);
+                    p:= p + char(b);
+                    inc(i, 3)
+                    end;
+                end;
+
+            if i < length(l) then
+                begin
+                l:= copy(l, i + 1, length(l) - i);
+                if l <> 'default' then
+                    begin
+                    p:= cmd + ' ' + l + ' ' + p;
+                    ParseCommand(p, true)
+                    end
+                end
+            end;
+
+        pfsClose(f)
+        end 
+        else
+            AddFileLog('[BINDS] file not found');
+end;
+
+
+procedure addBind(var binds: TBinds; var id: shortstring);
 var KeyName, Modifier, tmp: shortstring;
-    b: LongInt;
+    i, b: LongInt;
 begin
 KeyName:= '';
 Modifier:= '';
 
-if (not usingDBinds) then
-    begin
-    usingDBinds:= true;
-    FillByte(DefaultBinds, SizeOf(DefaultBinds), 0);
-    end;
-
-if (Pos('mod:', id) <> 0) then
+if(Pos('mod:', id) <> 0)then
     begin
     tmp:= '';
     SplitBySpace(id, tmp);
@@ -479,12 +528,27 @@
 if b = 0 then
     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
 else
-    DefaultBinds[b]:= KeyName;
+    begin 
+    // add bind: first check if this cmd is already bound, and remove old bind
+    i:= cKbdMaxIndex;
+    repeat
+        dec(i)
+    until (i < 0) or (binds[i] = KeyName);
+    if (i >= 0) then
+        binds[i]:= '';
+
+    binds[b]:= KeyName;
+    end
+end;
+
+// Bind that isn't a team bind, but overrides defaultbinds.
+procedure chDefaultBind(var id: shortstring);
+begin
+    addBind(DefaultBinds, id)
 end;
 
 procedure initModule;
 begin
-    usingDBinds:= false;
     RegisterVariable('dbind', @chDefaultBind, true );
 end;
 
--- a/hedgewars/uPhysFSLayer.pas	Sat Sep 21 23:54:57 2013 +0400
+++ b/hedgewars/uPhysFSLayer.pas	Fri Sep 27 00:54:18 2013 +0400
@@ -143,10 +143,8 @@
 
     i:= PHYSFS_mount(Str2PChar(PathPrefix), nil, false);
     AddFileLog('[PhysFS] mount ' + PathPrefix + ': ' + inttostr(i));
-    i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Data'), nil, false);
-    AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Data: ' + inttostr(i));
-    i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/Teams'), '/Teams', false);
-    AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/Teams: ' + inttostr(i));
+    i:= PHYSFS_mount(Str2PChar(UserPathPrefix + '/'), '/', false);
+    AddFileLog('[PhysFS] mount ' + UserPathPrefix + '/: ' + inttostr(i));
 
     hedgewarsMountPackages;
 end;
--- a/hedgewars/uTeams.pas	Sat Sep 21 23:54:57 2013 +0400
+++ b/hedgewars/uTeams.pas	Fri Sep 27 00:54:18 2013 +0400
@@ -43,7 +43,7 @@
 
 implementation
 uses uLocale, uAmmos, uChat, uVariables, uUtils, uIO, uCaptions, uCommands, uDebug,
-    uGearsUtils, uGearsList, uVisualGearsList, uPhysFSLayer
+    uGearsUtils, uGearsList, uVisualGearsList
     {$IFDEF USE_TOUCH_INTERFACE}, uTouch{$ENDIF};
 
 var MaxTeamHealth: LongInt;
@@ -570,57 +570,13 @@
 
 procedure loadTeamBinds(s: shortstring);
 var i: LongInt;
-    f: PFSFile;
-    p, l: shortstring;
-    b: byte;
 begin
-    l:= '';
-    
     for i:= 1 to length(s) do
         if s[i] in ['\', '/', ':'] then s[i]:= '_';
-        
-    s:= cPathz[ptTeams] + '/' + s + '.hwt';
-    if pfsExists(s) then
-        begin
-        AddFileLog('Loading binds from: ' + s);
-        f:= pfsOpenRead(s);
-        while (not pfsEOF(f)) and (l <> '[Binds]') do
-            pfsReadLn(f, l);
-
-        while (not pfsEOF(f)) and (l <> '') do
-            begin
-            pfsReadLn(f, l);
 
-            p:= '';
-            i:= 1;
-            while (i <= length(l)) and (l[i] <> '=') do
-                begin
-                if l[i] <> '%' then
-                    begin
-                    p:= p + l[i];
-                    inc(i)
-                    end else
-                    begin
-                    l[i]:= '$';
-                    val(copy(l, i, 3), b);
-                    p:= p + char(b);
-                    inc(i, 3)
-                    end;
-                end;
+    s:= cPathz[ptTeams] + '/' + s + '.hwt';
 
-            if i < length(l) then
-                begin
-                l:= copy(l, i + 1, length(l) - i);
-                if l <> 'default' then
-                    begin
-                    p:= 'bind ' + l + ' ' + p;
-                    ParseCommand(p, true)
-                    end
-                end
-            end;
-
-        pfsClose(f)
-        end
+    loadBinds('bind', s);
 end;
 
 procedure chAddTeam(var s: shortstring);
@@ -665,43 +621,11 @@
 end;
 
 procedure chBind(var id: shortstring);
-var KeyName, Modifier, tmp: shortstring;
-    i, b: LongInt;
 begin
-KeyName:= '';
-Modifier:= '';
-
-if CurrentTeam = nil then
-    exit;
-
-if(Pos('mod:', id) <> 0)then
-    begin
-    tmp:= '';
-    SplitBySpace(id, tmp);
-    Modifier:= id;
-    id:= tmp;
-    end;
+    if CurrentTeam = nil then
+        exit;
 
-SplitBySpace(id, KeyName);
-if KeyName[1]='"' then
-    Delete(KeyName, 1, 1);
-if KeyName[byte(KeyName[0])]='"' then
-    Delete(KeyName, byte(KeyName[0]), 1);
-b:= KeyNameToCode(id, Modifier);
-if b = 0 then
-    OutError(errmsgUnknownVariable + ' "' + id + '"', false)
-else
-    begin 
-    // add bind: first check if this cmd is already bound, and remove old bind
-    i:= cKbdMaxIndex;
-    repeat
-        dec(i)
-    until (i < 0) or (CurrentTeam^.Binds[i] = KeyName);
-    if (i >= 0) then
-        CurrentTeam^.Binds[i]:= '';
-
-    CurrentTeam^.Binds[b]:= KeyName;
-    end
+    addBind(CurrentTeam^.Binds, id)
 end;
 
 procedure chTeamGone(var s:shortstring);
--- a/project_files/hedgewars.pro	Sat Sep 21 23:54:57 2013 +0400
+++ b/project_files/hedgewars.pro	Fri Sep 27 00:54:18 2013 +0400
@@ -87,7 +87,6 @@
     ../QTfrontend/achievements.h \
     ../QTfrontend/binds.h \
     ../QTfrontend/ui_hwform.h \
-    ../QTfrontend/KB.h \
     ../QTfrontend/hwconsts.h \
     ../QTfrontend/sdlkeys.h \
     ../QTfrontend/ui/mouseoverfilter.h \