2012-08-24 17 views
3

我試圖製作一個JavaScript模擬器,並且我希望它非常輕薄,所以我不想用jQuery加載「ROMs」 jDataView。我用純JS製作了我自己的ROM加載器。使用JavaScript從二進制文件中讀取字節,沒有jQuery

它工作得很好(感謝這個網站上的很多主題),但IE瀏覽器仍然存在問題,爲此我在其他地方找不到任何幫助。

這裏是我的JS代碼:

/** 
* @param file - the path or URL of the ROM file to load. The file must be served with the Mime-Type: 'text/plain; charset=x-user-defined' 
* @param callback - a function to call when the loading is complete. Its first parameter contains an array of numbers representing the ROM bytes. 
*/ 
function loadRom(file, callback) 
{ 
    var xhr = new XMLHttpRequest();        // AJAX loader 
    xhr.onreadystatechange = function(){ 
    if(xhr.readyState == 4){         // When the file content is received 
     var str = xhr.responseText;        // Store it as a string 
     var ch, bytes = []; 
     for (var i = 0; i < str.length; i++){ 
     ch = str.charCodeAt(i);        // Read each character 
     bytes.push(ch & 0xFF);        // Store the last byte of the character 
     } 
     callback(bytes);          // Call the callback function with bytes as parameter 
    } 
    }; 
    xhr.open("GET", file, true);        // Load the file 
    xhr.send(null);            // Send the AJAX request 
} 

// Test 
var mem=[]; 
loadRom("TEST.ch8", function(m){ 
    for(i=0;i<m.length;i++)console.log(m[i].toString(16))     // Log each byte in hexa 
}); 

這裏是我的.htaccess(在chip8 ROM的的擴展.ch8):

AddType 'text/plain; charset=x-user-defined' ch8 

這是我的測試ROM(它包含字節0x00到爲0xFF)

http://www.filedropper.com/test_16

測試的結果:

Firefox和Chrome很好地工作:他們從0x00記錄每一個字節爲0xFF

IE9不相同,除了0x80的之間的32個字節0x9F,則與風馬牛不相及的數字代替。 (即使我們比較二進制代碼,也沒有明顯的轉換邏輯)

所以我的問題是:

  • 是什麼IE9做這些字節?
  • 我該如何解決?

感謝您的想法(或解決方案)!

最大。

+0

我已經玩了一下你的代碼段爲我自己的目的,如果你不介意我這樣做,但遇到了一些問題:http://stackoverflow.com/questions/22576518 –

回答

1

你有沒有考慮base-64編碼數據並在客戶端解碼它?

+0

是,我已經看到並考慮過涉及base64和/或PHP的解決方案,但我想要一個純JS解決方案。或者至少說明這些角色在IE9上發生了什麼。 –

+0

你可以在Javascript中解碼base64。請參閱http:// stackoverflow。com/questions/2820249/how-do-i-base64-encode-and-decode-using-javascript for example。不過,我很高興你發現了一些可行的方法。 –

+0

謝謝:) –

1

我終於找到了答案:

  • IE這些字符,轉換不管你做什麼。

  • 但它提供了一種直接導出的字節陣列的AJAX響應,這就是幾乎比其他瀏覽器

    變種字節= VBArray的(xhr.responseBody).toArray()冷卻器; //只在IE上!

所以這裏的函數將文件轉換爲字節數組,下降到IE7!

function loadRom(path, memory, callback) 
{ 
    var i = 0,                      // Loop iterator 
     ie /*@[email protected]*/,                    // IE detection with conditional compilation 
     xhr = new XMLHttpRequest;                  // XHR object 
    xhr.onreadystatechange = function(){                // When the XHR object state changes 
    if(xhr.readyState > 3){                   // When the file is received (readyState 4) 
     for(xhr = ie ? VBArray(xhr.responseBody).toArray() : xhr.responseText; i < xhr.length; i++){ // Get the response text as a bytes array (on IE) or a string (on other browsers) and iterate 
     memory.push(ie ? xhr[i] : xhr.charCodeAt(i) & 0xFF);          // Store in memory the byte (on IE) or the last byte of the character code (on other browsers) 
     } 
     callback()                     // Call the callback function 
    } 
    } 
    xhr.open("GET", path);                   // Load the file 
    xhr.send()                      // Send the XHR request 
} 
+0

'VBArray'可能不是你想要使用的東西。 JS現在已經鍵入了數組,並且它們得到了各種瀏覽器的很好的支持。請參閱http://www.html5rocks.com/en/tutorials/file/xhr2/ – ebidel