~swag/armagetronad/0.2.9-sty+ct+ap-fork

« back to all changes in this revision

Viewing changes to src/tools/tConfiguration.h

  • Committer: SwagTron
  • Date: 2019-03-05 07:13:03 UTC
  • Revision ID: swagtron-20190305071303-5m4cn9hs6h8ydsgf
- Ported toggle from 0.4. Seems kind of hacky. Had to add another FindConfigItem of type tConfItemBase due to tab completion using the previous tString function. Plan to change this later, but I really needed the toggle function. Tried to cast the two, but that did not work out. Seemed like the bug-less option for the time being. Those unfamiliar with this function, you can now toggle boolean commands (Ex. toggle text_out)

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include "tException.h"
35
35
#include "tLocale.h"
36
36
#include "tConsole.h"
37
 
#include "tLocale.h"
 
37
#include "tLocale.h"
38
38
#include "../network/nNetObject.h"
39
39
#include <iostream>
40
40
#include <ctype.h>
155
155
 
156
156
//! access levels for admin interfaces; lower numeric values are better
157
157
enum tAccessLevel
158
 
{
159
 
    tAccessLevel_Hoster = -1,      // the server hoster
 
158
{
 
159
    tAccessLevel_Hoster = -1,      // the server hoster
160
160
    tAccessLevel_Owner = 0,        // the server owner
161
161
    tAccessLevel_Admin = 1,        // one of his admins
162
162
    tAccessLevel_Moderator = 2,    // one of the moderators
185
185
    tAccessLevel_24 = 24,          // reserved
186
186
    tAccessLevel_25 = 25,          // reserved
187
187
    tAccessLevel_Invalid = 255,    // completely invalid level
188
 
    tAccessLevel_Default = 20,     // default (program)
 
188
    tAccessLevel_Default = 20,     // default (program)
189
189
    tAccessLevel_Punished = 21     // punished
190
190
};
191
191
 
222
222
    tCurrentAccessLevel( tCurrentAccessLevel const & );
223
223
    tCurrentAccessLevel & operator = ( tCurrentAccessLevel const & );
224
224
 
225
 
    tAccessLevel lastLevel_; //!< used to restore the last admin level when the object goes out of scope
 
225
    tAccessLevel lastLevel_; //!< used to restore the last admin level when the object goes out of scope
226
226
public:
227
227
    static tAccessLevel currentLevel_; //!< the current access level
228
228
};
236
236
    int id;
237
237
protected:
238
238
    const tString title;
239
 
    const tOutput help;
 
239
    const tOutput help;
240
240
    //  class tString value;
241
241
    bool changed;
242
242
 
247
247
    // the map of all configuration items
248
248
    typedef std::map< tString, tConfItemBase * > tConfItemMap;
249
249
    static tConfItemMap const & GetConfItemMap();
 
250
 
250
251
protected:
251
252
    static tConfItemMap & ConfItemMap();
 
253
 
 
254
 
 
255
// Lets steal this from 0.4 for now.
 
256
public:
 
257
    typedef void callbackFunc(void);
 
258
private:
 
259
    callbackFunc *callback;
 
260
protected:
 
261
    void ExecuteCallback() {if (callback != 0) (*callback)(); }
 
262
 
252
263
public:
253
264
 
254
265
    // static tConfItemBase* s_ConfItemAnchor;
255
266
    //static tConfItemBase* Anchor(){return dynamic_cast<tConfItemBase *>(s_ConfItemAnchor);}
256
267
    static bool printChange; //!< if set, setting changes are printed to the console and, if printErrors is set as well, suggestions of typo fixes are given.
257
268
    static bool printErrors; //!< if set, unknown settings are pointed out.
258
 
 
 
269
 
259
270
    tConfItemBase(const char *title, const tOutput& help);
260
 
    tConfItemBase(const char *title);
 
271
    tConfItemBase(const char *title);
261
272
 
262
273
    virtual ~tConfItemBase();
263
274
 
264
275
    tString const & GetTitle() const {
265
276
        return title;
266
 
    }
267
 
 
268
 
    /*void SetValue(tString newValue)
269
 
    {
270
 
        value = "";
271
 
        value << newValue;
 
277
    }
 
278
 
 
279
    /*void SetValue(tString newValue)
 
280
    {
 
281
        value = "";
 
282
        value << newValue;
272
283
    }*/
273
284
 
274
285
    tAccessLevel GetRequiredLevel() const { return requiredLevel; }
275
286
    tAccessLevel GetSetLevel() const { return setLevel; }
276
 
    void         SetSetLevel( tAccessLevel level ) { setLevel = level; }
 
287
    void         SetSetLevel( tAccessLevel level ) { setLevel = level; }
277
288
 
278
289
    static int EatWhitespace(std::istream &s); // eat whitespace from stream; return: first non-whitespace char
279
290
 
280
 
    static void ExportAll();
 
291
    static void ExportAll();
281
292
    static void SaveAll(std::ostream &s, bool all=false);
282
293
    static void LoadAll(std::istream &s, bool record = false );  //! loads configuration from stream
283
294
    static void LoadAll(std::ifstream &s, bool record = false );  //! loads configuration from file
284
295
    static void LoadLine(std::istream &s); //! loads one configuration line
285
296
    static bool LoadPlayback( bool print = false ); //! loads configuration from playback
286
297
    static void DocAll(std::ostream &s);
287
 
    static int AccessLevel(std::istream &s); //! Returns access level needed for command. -1 if command not found.
288
 
    static void WriteAllToFile();
289
 
    static void WriteAllLevelsToFile();
290
 
    static void WriteChangedToFile();
291
 
    static tString FindConfigItem(tString name);    //! Returns the config name of the searching string name
292
 
    static void SetAllAccessLevel(int newLevel);
293
 
 
294
 
    static void DownloadSettings_Go(nMessage &m);
295
 
    static void DownloadConfig_Go(nMessage &m);
296
 
    static void DownloadSettings_To(int peer);
 
298
    static int AccessLevel(std::istream &s); //! Returns access level needed for command. -1 if command not found.
 
299
    static void WriteAllToFile();
 
300
    static void WriteAllLevelsToFile();
 
301
    static void WriteChangedToFile();
 
302
    static tString FindConfigItem(tString name);    //! Returns the config name of the searching string name
 
303
    static tConfItemBase *FindConfigItem2(tString const &name); // FindConfigitem specifically for the use of "TOGGLE." Gotta replace this. Seems too hacky.
 
304
    static void SetAllAccessLevel(int newLevel);
 
305
 
 
306
    static void DownloadSettings_Go(nMessage &m);
 
307
    static void DownloadConfig_Go(nMessage &m);
 
308
    static void DownloadSettings_To(int peer);
297
309
    static void DownloadConfig_To(tString file, int peer);
298
310
 
299
311
    // helper functions for files (use these, they manage recording and playback properly)
308
320
    static void ReadFile( std::ifstream & s ); //! loads configuration from a file
309
321
 
310
322
    virtual void ReadVal(std::istream &s)=0;
311
 
    virtual void WriteVal(std::ostream &s)=0;
 
323
    virtual void WriteVal(std::ostream &s)=0;
312
324
    virtual void FetchVal(tString &val)=0;
313
325
 
314
326
    virtual void WasChanged(){} // what to do if a read changed the thing
396
408
    tConfItem(T &t):tConfItemBase(""),target(&t), shouldChangeFunc_(NULL) {}
397
409
public:
398
410
    tConfItem(const char *title,const tOutput& help,T& t)
399
 
            :tConfItemBase(title,help),target(&t), shouldChangeFunc_(NULL) {
400
 
                /*tConfItemMap & confmap = ConfItemMap();
 
411
            :tConfItemBase(title,help),target(&t), shouldChangeFunc_(NULL) {
 
412
                /*tConfItemMap & confmap = ConfItemMap();
401
413
                for(tConfItemMap::iterator iter = confmap.begin(); iter != confmap.end() ; ++iter)
402
 
                {
403
 
                    tConfItemBase * ci = (*iter).second;
404
 
                    if (ci->GetTitle() == title)
405
 
                    {
406
 
                        tString newValue;
407
 
                        newValue << *target;
408
 
                        ci->SetValue(newValue);
409
 
                        break;
410
 
                    }
411
 
                }*/
 
414
                {
 
415
                    tConfItemBase * ci = (*iter).second;
 
416
                    if (ci->GetTitle() == title)
 
417
                    {
 
418
                        tString newValue;
 
419
                        newValue << *target;
 
420
                        ci->SetValue(newValue);
 
421
                        break;
 
422
                    }
 
423
                }*/
412
424
            }
413
425
 
414
426
    tConfItem(const char *title,T& t)
415
 
            :tConfItemBase(title),target(&t), shouldChangeFunc_(NULL) {
416
 
                /*tConfItemMap & confmap = ConfItemMap();
 
427
            :tConfItemBase(title),target(&t), shouldChangeFunc_(NULL) {
 
428
                /*tConfItemMap & confmap = ConfItemMap();
417
429
                for(tConfItemMap::iterator iter = confmap.begin(); iter != confmap.end() ; ++iter)
418
 
                {
419
 
                    tConfItemBase * ci = (*iter).second;
420
 
                    if (ci->GetTitle() == title)
421
 
                    {
422
 
                        tString newValue;
423
 
                        newValue << *target;
424
 
                        ci->SetValue(newValue);
425
 
                        break;
426
 
                    }
427
 
                }*/
 
430
                {
 
431
                    tConfItemBase * ci = (*iter).second;
 
432
                    if (ci->GetTitle() == title)
 
433
                    {
 
434
                        tString newValue;
 
435
                        newValue << *target;
 
436
                        ci->SetValue(newValue);
 
437
                        break;
 
438
                    }
 
439
                }*/
428
440
            }
429
441
 
430
442
    tConfItem(const char*title, T& t, ShouldChangeFuncT changeFunc)
431
 
            :tConfItemBase(title),target(&t),shouldChangeFunc_(changeFunc) {
432
 
                /*tConfItemMap & confmap = ConfItemMap();
 
443
            :tConfItemBase(title),target(&t),shouldChangeFunc_(changeFunc) {
 
444
                /*tConfItemMap & confmap = ConfItemMap();
433
445
                for(tConfItemMap::iterator iter = confmap.begin(); iter != confmap.end() ; ++iter)
434
 
                {
435
 
                    tConfItemBase * ci = (*iter).second;
436
 
                    if (ci->GetTitle() == title)
437
 
                    {
438
 
                        tString newValue;
439
 
                        newValue << *target;
440
 
                        ci->SetValue(newValue);
441
 
                        break;
442
 
                    }
443
 
                }*/
 
446
                {
 
447
                    tConfItemBase * ci = (*iter).second;
 
448
                    if (ci->GetTitle() == title)
 
449
                    {
 
450
                        tString newValue;
 
451
                        newValue << *target;
 
452
                        ci->SetValue(newValue);
 
453
                        break;
 
454
                    }
 
455
                }*/
444
456
            }
445
457
 
446
458
    virtual ~tConfItem(){}
480
492
        s << static_cast< typename tTypeToConfig< T >::TOSTREAM >( value );
481
493
    }
482
494
 
 
495
    //Stolen from 0.4. Needed for "TOGGLE" function.
 
496
    void SetVal( T const & val )
 
497
    {
 
498
        if (!shouldChangeFunc_ || shouldChangeFunc_( val ))
 
499
        {
 
500
            if (printChange)
 
501
            {
 
502
                tOutput o;
 
503
                o.SetTemplateParameter(1, title);
 
504
                o.SetTemplateParameter(2, *target);
 
505
                o.SetTemplateParameter(3, val);
 
506
                o << "$config_value_changed";
 
507
                con << o;
 
508
            }
 
509
            
 
510
            *target = val;
 
511
            changed = true;
 
512
        }
 
513
        else
 
514
        {
 
515
            con << tOutput("$config_value_not_changed", title, *target, val);
 
516
        }
 
517
        ExecuteCallback();    
 
518
    }
 
519
    
 
520
    const T *GetTarget() const
 
521
    {
 
522
        return target;
 
523
    }
 
524
    
 
525
 
483
526
    virtual void ReadVal(std::istream &s){
484
527
        // eat whitepsace
485
528
        int c= EatWhitespace(s);
542
585
 
543
586
    virtual void WriteVal(std::ostream &s){
544
587
        DoWrite( s, *target, DUMMYREQUIRED() );
545
 
    }
546
 
 
547
 
    virtual void FetchVal(tString &val)
548
 
    {
549
 
        val << *target;
 
588
    }
 
589
 
 
590
    virtual void FetchVal(tString &val)
 
591
    {
 
592
        val << *target;
550
593
    }
551
594
};
552
595
 
591
634
    virtual ~tConfItemFunc();
592
635
 
593
636
    virtual void ReadVal(std::istream &s);
594
 
    virtual void WriteVal(std::ostream &s);
 
637
    virtual void WriteVal(std::ostream &s);
595
638
    virtual void FetchVal(tString &val);
596
639
 
597
 
    virtual bool Save();
598
 
 
599
 
    virtual bool CanSave(){
600
 
        return false;
 
640
    virtual bool Save();
 
641
 
 
642
    virtual bool CanSave(){
 
643
        return false;
601
644
    }
602
645
};
603
 
 
604
 
// includes a single configuration file by name, searches in var and config directories
605
 
void st_Include( tString const & file );
 
646
 
 
647
// includes a single configuration file by name, searches in var and config directories
 
648
void st_Include( tString const & file );
606
649
 
607
650
void st_LoadConfig();
608
 
void st_SaveConfig();
609
 
void st_LoadUserConfig();
 
651
void st_SaveConfig();
 
652
void st_LoadUserConfig();
610
653
void st_SaveChangedConfig();
611
654
 
612
655
extern bool st_FirstUse;