Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "stdafx.h"

#include "Headers/Localization.h"

#include "Core/Headers/ErrorCodes.h"
#include <SimpleINI/include/SimpleIni.h>

#include "Platform/File/Headers/FileManagement.h"
#include "Platform/File/Headers/FileUpdateMonitor.h"

namespace Divide {
namespace Locale {

namespace {
    CSimpleIni g_languageFile;
    constexpr char* g_languageFileExtension = ".ini";

    /// External modification monitoring system
    std::unique_ptr<FW::FileWatcher> s_LanguageFileWatcher = nullptr;

    UpdateListener s_fileWatcherListener([](const char* languageFile, FileUpdateEvent evt) {
        detail::onLanguageFileModify(languageFile, evt);
    });
};

namespace detail {
    void onLanguageFileModify(const char* languageFile, FileUpdateEvent evt) {
        if (evt == FileUpdateEvent::DELETE) {
            return;
        }

        // If we modify our currently active language, reinit the Locale system
        if (strcmp((s_localeFile + g_languageFileExtension).c_str(), languageFile) == 0) {
            changeLanguage(s_localeFile);
        }
    }
};

ErrorCode init(const stringImpl& newLanguage) {
    clear();
    if (!Config::Build::IS_SHIPPING_BUILD) {
        if (!s_LanguageFileWatcher) {
            s_LanguageFileWatcher.reset(new FW::FileWatcher());
            s_fileWatcherListener.addIgnoredEndCharacter('~');
            s_fileWatcherListener.addIgnoredExtension("tmp");
            s_LanguageFileWatcher->addWatch(Paths::g_localisationPath.c_str(), &s_fileWatcherListener);
        }
    }

    detail::s_localeFile = newLanguage;
    // Use SimpleIni library for cross-platform INI parsing
    g_languageFile.SetUnicode();
    g_languageFile.SetMultiLine(true);

    stringImpl file = Paths::g_localisationPath + detail::s_localeFile + g_languageFileExtension;

    if (g_languageFile.LoadFile(file.c_str()) != SI_OK) {
        return ErrorCode::NO_LANGUAGE_INI;
    }

    // Load all key-value pairs for the "language" section
    const CSimpleIni::TKeyVal* keyValue = g_languageFile.GetSection("language");

    assert(keyValue != nullptr &&
           "Locale::init error: No 'language' section found");
    // And add all pairs to the language table
    CSimpleIni::TKeyVal::const_iterator keyValuePairIt = keyValue->begin();
    for (; keyValuePairIt != keyValue->end(); ++keyValuePairIt) {
        stringImpl value(keyValuePairIt->second);
        
        hashAlg::emplace(detail::s_languageTable,
                         _ID_RT(keyValuePairIt->first.pItem),
                         value);
    }

    detail::s_initialized = true;
    return ErrorCode::NO_ERR;
}

void clear() {
    detail::s_languageTable.clear();
    g_languageFile.Reset();
    detail::s_initialized = false;
}

void idle() {
    if (s_LanguageFileWatcher) {
        s_LanguageFileWatcher->update();
    }
}

/// Altough the language can be set at compile time, in-game options may support
/// language changes
void changeLanguage(const stringImpl& newLanguage) {
    /// Set the new language code
    init(newLanguage);

    for (const DELEGATE_CBK<void, const char* /*new language*/>& languageChangeCbk : detail::s_languageChangeCallbacks) {
        languageChangeCbk(newLanguage.c_str());
    }
}

const char* get(U64 key, const char* defaultValue) {
    if (detail::s_initialized) {
        typedef hashMapImpl<U64, stringImpl>::const_iterator citer;
        // When we ask for a string for the given key, we check our language cache first
        citer entry = detail::s_languageTable.find(key);
        if (entry != std::cend(detail::s_languageTable)) {
            // Usually, the entire language table is loaded.
            return entry->second.c_str();
        }
        DIVIDE_UNEXPECTED_CALL("Locale error: INVALID STRING KEY!");
    }

    return defaultValue;
}

const char* get(U64 key) {
    return get(key, "key not found");
}

void addChangeLanguageCallback(const DELEGATE_CBK<void, const char* /*new language*/>& cbk) {
    detail::s_languageChangeCallbacks.emplace_back(cbk);
}

};  // namespace Locale
};  // namespace Divide

Commits for Divide-Framework/trunk/Source Code/Utility/Localization.cpp

Diff revisions: vs.
Revision Author Commited Message
893 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 17:33:07 +0000

[Ionut]
- Initial implementation of a PipelineStateObject (holds shader program, rasterizer state, etc)
- Rework PCH implementation a bit because VS2017 no longer has a /ZM option

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

846 Diff Diff IonutCava picture IonutCava Fri 03 Feb, 2017 22:41:05 +0000

[IonutCava]
- More scripting work

845 Diff Diff IonutCava picture IonutCava Fri 03 Feb, 2017 14:36:26 +0000

[IonutCava]
- Hot reloading system cleanup
- More script system bug fixes

844 Diff Diff IonutCava picture IonutCava Thu 02 Feb, 2017 21:49:28 +0000

[IonutCava]
- More scripting prep work and experiments

842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

841 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 14:47:57 +0000

[IonutCava]
- Add callbacks to Localization so that listeners can be informed of language changes
— First callback added to GUIInterface

840 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 14:06:06 +0000

[IonutCava]
- Fix a bug with hot shader reloading and atom file caching
- Add hot language file reloading.
— Changes made to the current language file will be immediately applied

835 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:58:07 +0000

[IonutCava]
- Split Engine lib into Core and Engine lib.
- Fix Server build issues

832 IonutCava picture IonutCava Thu 26 Jan, 2017 17:23:11 +0000

[IonutCava]
- More path related work
- Initial code to centralize file I/O (does not compile yet)